package com.sinog.alarm.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.metadata.Sheet;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.util.CollectionUtils;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.sinog.alarm.AlarmApplication;
import com.sinog.alarm.easyexcel.ExcelListener;
import com.sinog.alarm.easyexcel.ZoneInfo;
import com.sinog.alarm.mapper.db2.AuthUserMapper;
import com.sinog.alarm.mapper.db2.CommonMapper;
import com.sinog.alarm.mapper.db2.DepartmentMapper;
import com.sinog.alarm.mapper.db2.SystemCodeMapper;
import com.sinog.alarm.mapper.db2.SystemUserMapper;
import com.sinog.alarm.mapper.db2.ZoningMapper;
import com.sinog.alarm.model.AuthUser;
import com.sinog.alarm.model.Department;
import com.sinog.alarm.model.SystemCode;
import com.sinog.alarm.model.SystemUser;
import com.sinog.alarm.service.ZoningService;
import com.sinog.alarm.util.Constants;
import com.sinog.alarm.util.FileUtil;
import com.sinog.alarm.util.JsonUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.sql.CallableStatement;
import java.sql.Types;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangxj
 * @desc ZoningServiceImpl
 * @date 2022/2/21 18:06
 */
@Slf4j
@Service
public class ZoningServiceImpl implements ZoningService {

    @Value("${local_code.code}")
    private String localCode;
    @Value("${azbj.zonePath.insert}")
    private String insertFilePath;
    @Value("${azbj.zonePath.update}")
    private String updateFilePath;
    @Value("${azbj.zonePath.merge}")
    private String mergeFilePath;
    @Value("${azbj.zonePath.transfer}")
    private String transferFilePath;
    @Value("${azbj.zonePath.delete}")
    private String deleteFilePath;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private SystemCodeMapper systemCodeMapper;
    @Resource
    private SystemUserMapper systemUserMapper;
    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private AuthUserMapper authUserMapper;
    @Resource
    private ZoningMapper zoningMapper;
    @Resource
    private CommonMapper commonMapper;
    private JdbcTemplate jdbcTemplate;
    // --------------------------------------------默认创建用户密码:asdf@123
    private static final String password = "$2a$10$1cOfgZ0V6VtlmUW20BWFeOPs9iu.E.LlCk9fd5SPolYZzTJAThO9K";

    /**
     * 行政区划合并功能
     */
    @Override
    public void zoningConsolidation() {
        String fileName = null;
        InputStream inputStream = null;
        AnalysisContext analysisContext = null;
        AnalysisContext analysisContext1 = null;
        AnalysisContext analysisContext2 = null;
        ApplicationContext applicationContext = AlarmApplication.applicationContext;
        if(!ObjectUtils.allNotNull(jdbcTemplate)) {
            DataSource db2DataSource = (DataSource)applicationContext.getBean("db2DataSource");
            jdbcTemplate = new JdbcTemplate(db2DataSource);
        }
        SimpleDateFormat simformat = new SimpleDateFormat("yyyyMMdd");
        String dateformat = simformat.format(new Date());
        long uuid = stringRedisTemplate.opsForHash().increment("uuid",dateformat,1L);
        log.info("行政区划合并,生成uuid:{}",dateformat + uuid);
        ExcelListener listener = new ExcelListener(dateformat + uuid);
        // 1. 方法整体描述
        // 1.1 从/opt/insert目录下读取所有excel文档,读取所需要插入的文档进行逐行解析
        List<File> insertFile = FileUtil.getAllFile(insertFilePath);
        if(null != insertFile) {
            for(File file : insertFile) {
                try {
                    // 1.1.1 新增地市获取excel第1个sheet页获取:省名称,地市名称;
                    fileName = file.getName();
                    byte[] fileByte = FileUtil.getFileByte(file);
                    inputStream = new ByteArrayInputStream(fileByte);
                    Sheet sheet = new Sheet(1,1,ZoneInfo.class);
                    //开始读取
                    ExcelReader excelReader = new ExcelReader(inputStream,ExcelTypeEnum.XLSX,null,listener);
                    excelReader.read(sheet);
                    analysisContext = excelReader.analysisContext();
                    // 1.1.2 新增区县获取excel第2个sheet页获取:省名称,地市名称,区县名称;
                    byte[] fileByte2 = FileUtil.getFileByte(file);
                    inputStream = new ByteArrayInputStream(fileByte2);
                    Sheet sheet2 = new Sheet(2,1,ZoneInfo.class);
                    //开始读取
                    ExcelReader excelReader1 = new ExcelReader(inputStream,ExcelTypeEnum.XLSX,null,listener);
                    excelReader1.read(sheet2);
                    analysisContext1 = excelReader1.analysisContext();
                    // 1.1.3 新增乡镇/街道获取excel第3个sheet页获取:省名称,地市名称,区县名称,街道名称;
                    byte[] fileByte3 = FileUtil.getFileByte(file);
                    inputStream = new ByteArrayInputStream(fileByte3);
                    Sheet sheet3 = new Sheet(3,1,ZoneInfo.class);
                    //开始读取
                    ExcelReader excelReader2 = new ExcelReader(inputStream,ExcelTypeEnum.XLSX,null,listener);
                    excelReader2.read(sheet3);
                    analysisContext2 = excelReader2.analysisContext();
                } catch(IOException e) {
                    log.debug("发生异常:{}",e);
                } finally {
                    // 执行完毕后将file 删除
                    if(file.exists()) {
                        // 将文件移动到bak目录下
                        FileUtil.copyFile(file.getPath(),insertFilePath + File.separator + "bak",dateformat + uuid + file.getName());
                        file.delete();
                    }
                    // 从redis中获取excel执行数据,将信息写回到excel中
                    if(stringRedisTemplate.hasKey("area_merge")) {
                        ExcelWriter excelWriter = null;
                        try {
                            // 这里 指定文件
                            excelWriter = EasyExcel.write(insertFilePath + File.separator + "bak" + File.separator + fileName)
                                                   .build();
                            Integer sheetNo = analysisContext.readSheetHolder().getSheetNo();
                            Integer sheetNo1 = analysisContext1.readSheetHolder().getSheetNo();
                            Integer sheetNo2 = analysisContext2.readSheetHolder().getSheetNo();
                            if(Constants.Number.NUM_ZERO.equals(sheetNo)) {
                                List<ZoneInfo> resultSheetInfos = JsonUtil.toList((String)stringRedisTemplate.opsForHash()
                                                                                                             .get("area_merge",dateformat + uuid + ":" + analysisContext.getCurrentSheet()
                                                                                                                                                                        .getSheetNo() + JsonUtil.toText(analysisContext.readRowHolder()
                                                                                                                                                                                                                       .getCurrentRowAnalysisResult())),ZoneInfo.class);
                                // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样。这里注意DemoData.class 可以每次都变，我这里为了方便 所以用的同一个class 实际上可以一直变
                                WriteSheet writeSheet = EasyExcel.writerSheet(1,"新增地市").head(ZoneInfo.class).build();
                                // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
                                excelWriter.write(resultSheetInfos,writeSheet);
                            }
                            if(Constants.Number.NUM_ONE.equals(sheetNo1)) {
                                List<ZoneInfo> resultSheetInfos = JsonUtil.toList((String)stringRedisTemplate.opsForHash()
                                                                                                             .get("area_merge",dateformat + uuid + ":" + analysisContext1.getCurrentSheet()
                                                                                                                                                                         .getSheetNo() + JsonUtil.toText(analysisContext1.readRowHolder()
                                                                                                                                                                                                                         .getCurrentRowAnalysisResult())),ZoneInfo.class);
                                // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样。这里注意DemoData.class 可以每次都变，我这里为了方便 所以用的同一个class 实际上可以一直变
                                WriteSheet writeSheet = EasyExcel.writerSheet(2,"新增区县").head(ZoneInfo.class).build();
                                // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
                                excelWriter.write(resultSheetInfos,writeSheet);
                            }
                            if(Constants.Number.NUM_TWO.equals(sheetNo2)) {
                                List<ZoneInfo> resultSheetInfos = JsonUtil.toList((String)stringRedisTemplate.opsForHash()
                                                                                                             .get("area_merge",dateformat + uuid + ":" + analysisContext2.getCurrentSheet()
                                                                                                                                                                         .getSheetNo() + JsonUtil.toText(analysisContext2.readRowHolder()
                                                                                                                                                                                                                         .getCurrentRowAnalysisResult())),ZoneInfo.class);
                                // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样。这里注意DemoData.class 可以每次都变，我这里为了方便 所以用的同一个class 实际上可以一直变
                                WriteSheet writeSheet = EasyExcel.writerSheet(3,"新增乡镇街道").head(ZoneInfo.class).build();
                                // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
                                excelWriter.write(resultSheetInfos,writeSheet);
                            }
                        } catch(Exception e) {
                            e.printStackTrace();
                        } finally {
                            // 千万别忘记finish 会帮忙关闭流
                            if(excelWriter != null) {
                                excelWriter.finish();
                            }
                        }
                    }
                }
            }
        }
        // 1.2 从/opt/update目录下读取所有excel文档,读取所需要修改的文档进行逐行解析
        List<File> updateFile = FileUtil.getAllFile(updateFilePath);
        if(null != updateFile) {
            for(File file : updateFile) {
                try {
                    fileName = file.getName();
                    // 1.2.1 修改乡镇/街道名称获取excel第1个sheet页获取:省名称,地市名称,区县名称,街道名称;
                    byte[] fileByte = FileUtil.getFileByte(file);
                    inputStream = new ByteArrayInputStream(fileByte);
                    Sheet sheet = new Sheet(1,1,ZoneInfo.class);
                    //开始读取
                    ExcelReader excelReader = new ExcelReader(inputStream,ExcelTypeEnum.XLSX,null,listener);
                    excelReader.read(sheet);
                    analysisContext = excelReader.analysisContext();
                    // 1.2.2 修改区县名称获取excel第2个sheet页获取:省名称,地市名称,区县名称;
                    byte[] fileByte2 = FileUtil.getFileByte(file);
                    inputStream = new ByteArrayInputStream(fileByte2);
                    Sheet sheet2 = new Sheet(2,1,ZoneInfo.class);
                    //开始读取
                    ExcelReader excelReader1 = new ExcelReader(inputStream,ExcelTypeEnum.XLSX,null,listener);
                    excelReader1.read(sheet2);
                    analysisContext1 = excelReader1.analysisContext();
                    // 1.2.3 修改地市名称获取excel第3个sheet页获取:省名称,地市名称;
                    byte[] fileByte3 = FileUtil.getFileByte(file);
                    inputStream = new ByteArrayInputStream(fileByte3);
                    Sheet sheet3 = new Sheet(3,1,ZoneInfo.class);
                    //开始读取
                    ExcelReader excelReader2 = new ExcelReader(inputStream,ExcelTypeEnum.XLSX,null,listener);
                    excelReader2.read(sheet3);
                    analysisContext2 = excelReader2.analysisContext();
                } catch(IOException e) {
                    log.debug("发生异常:{}",e);
                } finally {
                    // 执行完毕后将file 删除
                    if(file.exists()) {
                        // 将文件移动到bak目录下
                        FileUtil.copyFile(file.getPath(),updateFilePath + File.separator + "bak",dateformat + uuid + file.getName());
                        file.delete();
                    }
                    // 从redis中获取excel执行数据,将信息写回到excel中
                    if(stringRedisTemplate.hasKey("area_merge")) {
                        ExcelWriter excelWriter = null;
                        try {
                            // 这里 指定文件
                            excelWriter = EasyExcel.write(updateFilePath + File.separator + "bak" + File.separator + fileName)
                                                   .build();
                            Integer sheetNo = analysisContext.readSheetHolder().getSheetNo();
                            Integer sheetNo1 = analysisContext1.readSheetHolder().getSheetNo();
                            Integer sheetNo2 = analysisContext2.readSheetHolder().getSheetNo();
                            if(Constants.Number.NUM_ZERO.equals(sheetNo)) {
                                List<ZoneInfo> resultSheetInfos = JsonUtil.toList((String)stringRedisTemplate.opsForHash()
                                                                                                             .get("area_merge",dateformat + uuid + ":" + analysisContext.getCurrentSheet()
                                                                                                                                                                        .getSheetNo() + JsonUtil.toText(analysisContext.readRowHolder()
                                                                                                                                                                                                                       .getCurrentRowAnalysisResult())),ZoneInfo.class);
                                // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样。这里注意DemoData.class 可以每次都变，我这里为了方便 所以用的同一个class 实际上可以一直变
                                WriteSheet writeSheet = EasyExcel.writerSheet(3,"修改乡镇街道名称").head(ZoneInfo.class)
                                                                 .build();
                                // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
                                excelWriter.write(resultSheetInfos,writeSheet);
                            }
                            if(Constants.Number.NUM_ONE.equals(sheetNo1)) {
                                List<ZoneInfo> resultSheetInfos = JsonUtil.toList((String)stringRedisTemplate.opsForHash()
                                                                                                             .get("area_merge",dateformat + uuid + ":" + analysisContext1.getCurrentSheet()
                                                                                                                                                                         .getSheetNo() + JsonUtil.toText(analysisContext1.readRowHolder()
                                                                                                                                                                                                                         .getCurrentRowAnalysisResult())),ZoneInfo.class);
                                // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样。这里注意DemoData.class 可以每次都变，我这里为了方便 所以用的同一个class 实际上可以一直变
                                WriteSheet writeSheet = EasyExcel.writerSheet(2,"修改区县名称").head(ZoneInfo.class).build();
                                // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
                                excelWriter.write(resultSheetInfos,writeSheet);
                            }
                            if(Constants.Number.NUM_TWO.equals(sheetNo2)) {
                                List<ZoneInfo> resultSheetInfos = JsonUtil.toList((String)stringRedisTemplate.opsForHash()
                                                                                                             .get("area_merge",dateformat + uuid + ":" + analysisContext2.getCurrentSheet()
                                                                                                                                                                         .getSheetNo() + JsonUtil.toText(analysisContext2.readRowHolder()
                                                                                                                                                                                                                         .getCurrentRowAnalysisResult())),ZoneInfo.class);
                                // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样。这里注意DemoData.class 可以每次都变，我这里为了方便 所以用的同一个class 实际上可以一直变
                                WriteSheet writeSheet = EasyExcel.writerSheet(1,"修改地市名称").head(ZoneInfo.class).build();
                                // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
                                excelWriter.write(resultSheetInfos,writeSheet);
                            }
                        } catch(Exception e) {
                            e.printStackTrace();
                        } finally {
                            // 千万别忘记finish 会帮忙关闭流
                            if(excelWriter != null) {
                                excelWriter.finish();
                            }
                        }
                    }
                }
            }
        }
        // 1.3 从/opt/merge目录下读取所有excel文档,读取所需要合并的文档进行逐行解析
        List<File> mergeFile = FileUtil.getAllFile(mergeFilePath);
        if(null != mergeFile) {
            for(File file : mergeFile) {
                try {
                    fileName = file.getName();
                    // 1.3.1 获取合并的两方街道信息,以及合并后的名称
                    // 省名称,地市名称,区县名称,街道名称,省名称,地市名称,区县名称,街道名称,合并后的名称
                    byte[] fileByte = FileUtil.getFileByte(file);
                    inputStream = new ByteArrayInputStream(fileByte);
                    Sheet sheet = new Sheet(1,1,ZoneInfo.class);
                    //开始读取
                    ExcelReader excelReader = new ExcelReader(inputStream,ExcelTypeEnum.XLSX,null,listener);
                    excelReader.read(sheet);
                    analysisContext = excelReader.analysisContext();
                    // 省名称,地市名称,区县名称,省名称,地市名称,区县名称,合并后的名称
                    byte[] fileByte2 = FileUtil.getFileByte(file);
                    inputStream = new ByteArrayInputStream(fileByte2);
                    Sheet sheet2 = new Sheet(2,1,ZoneInfo.class);
                    //开始读取
                    ExcelReader excelReader1 = new ExcelReader(inputStream,ExcelTypeEnum.XLSX,null,listener);
                    excelReader1.read(sheet2);
                    analysisContext1 = excelReader1.analysisContext();
                } catch(IOException e) {
                    log.debug("发生异常:{}",e);
                } finally {
                    // 执行完毕后将file 删除
                    if(file.exists()) {
                        // 将文件移动到bak目录下
                        FileUtil.copyFile(file.getPath(),mergeFilePath + File.separator + "bak",dateformat + uuid + file.getName());
                        file.delete();
                    }
                    // 从redis中获取excel执行数据,将信息写回到excel中
                    if(stringRedisTemplate.hasKey("area_merge")) {
                        ExcelWriter excelWriter = null;
                        try {
                            // 这里 指定文件
                            excelWriter = EasyExcel.write(mergeFilePath + File.separator + "bak" + File.separator + fileName)
                                                   .build();
                            Integer sheetNo = analysisContext.readSheetHolder().getSheetNo();
                            Integer sheetNo1 = analysisContext1.readSheetHolder().getSheetNo();
                            if(Constants.Number.NUM_ZERO.equals(sheetNo)) {
                                List<ZoneInfo> resultSheetInfos = JsonUtil.toList((String)stringRedisTemplate.opsForHash()
                                                                                                             .get("area_merge",dateformat + uuid + ":" + analysisContext.getCurrentSheet()
                                                                                                                                                                        .getSheetNo() + JsonUtil.toText(analysisContext.readRowHolder()
                                                                                                                                                                                                                       .getCurrentRowAnalysisResult())),ZoneInfo.class);
                                // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样。这里注意DemoData.class 可以每次都变，我这里为了方便 所以用的同一个class 实际上可以一直变
                                WriteSheet writeSheet = EasyExcel.writerSheet(1,"乡镇街道行政区划合并").head(ZoneInfo.class)
                                                                 .build();
                                // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
                                excelWriter.write(resultSheetInfos,writeSheet);
                            }
                            if(Constants.Number.NUM_ONE.equals(sheetNo1)) {
                                List<ZoneInfo> resultSheetInfos = JsonUtil.toList((String)stringRedisTemplate.opsForHash()
                                                                                                             .get("area_merge",dateformat + uuid + ":" + analysisContext1.getCurrentSheet()
                                                                                                                                                                         .getSheetNo() + JsonUtil.toText(analysisContext1.readRowHolder()
                                                                                                                                                                                                                         .getCurrentRowAnalysisResult())),ZoneInfo.class);
                                // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样。这里注意DemoData.class 可以每次都变，我这里为了方便 所以用的同一个class 实际上可以一直变
                                WriteSheet writeSheet = EasyExcel.writerSheet(2,"区县行政区划合并").head(ZoneInfo.class)
                                                                 .build();
                                // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
                                excelWriter.write(resultSheetInfos,writeSheet);
                            }
                        } catch(Exception e) {
                            e.printStackTrace();
                        } finally {
                            // 千万别忘记finish 会帮忙关闭流
                            if(excelWriter != null) {
                                excelWriter.finish();
                            }
                        }
                    }
                }
            }
        }
        // 1.4 从/opt/transfer目录下读取所有excel文档,读取所需要合并的文档进行逐行解析
        List<File> transferFile = FileUtil.getAllFile(transferFilePath);
        if(null != transferFile) {
            for(File file : transferFile) {
                try {
                    fileName = file.getName();
                    // 1.3.1 获取合并的两方街道信息,以及合并后的名称
                    // 省名称,地市名称,区县名称,街道名称,省名称,地市名称,区县名称,街道名称,合并后的名称
                    byte[] fileByte = FileUtil.getFileByte(file);
                    inputStream = new ByteArrayInputStream(fileByte);
                    Sheet sheet = new Sheet(1,1,ZoneInfo.class);
                    //开始读取
                    ExcelReader excelReader = new ExcelReader(inputStream,ExcelTypeEnum.XLSX,null,listener);
                    excelReader.read(sheet);
                    analysisContext = excelReader.analysisContext();
                    // 省名称,地市名称,区县名称,省名称,地市名称,区县名称,合并后的名称
                    byte[] fileByte2 = FileUtil.getFileByte(file);
                    inputStream = new ByteArrayInputStream(fileByte2);
                    Sheet sheet2 = new Sheet(2,1,ZoneInfo.class);
                    //开始读取
                    ExcelReader excelReader1 = new ExcelReader(inputStream,ExcelTypeEnum.XLSX,null,listener);
                    excelReader1.read(sheet2);
                    analysisContext1 = excelReader1.analysisContext();
                } catch(IOException e) {
                    log.debug("发生异常:{}",e);
                } finally {
                    // 执行完毕后将file 删除
                    if(file.exists()) {
                        // 将文件移动到bak目录下
                        FileUtil.copyFile(file.getPath(),transferFilePath + File.separator + "bak",dateformat + uuid + file.getName());
                        file.delete();
                    }
                    // 从redis中获取excel执行数据,将信息写回到excel中
                    if(stringRedisTemplate.hasKey("area_merge")) {
                        ExcelWriter excelWriter = null;
                        try {
                            // 这里 指定文件
                            excelWriter = EasyExcel.write(transferFilePath + File.separator + "bak" + File.separator + fileName)
                                                   .build();
                            Integer sheetNo = analysisContext.readSheetHolder().getSheetNo();
                            Integer sheetNo1 = analysisContext1.readSheetHolder().getSheetNo();
                            if(Constants.Number.NUM_ZERO.equals(sheetNo)) {
                                List<ZoneInfo> resultSheetInfos = JsonUtil.toList((String)stringRedisTemplate.opsForHash()
                                                                                                             .get("area_merge",dateformat + uuid + ":" + analysisContext.getCurrentSheet()
                                                                                                                                                                        .getSheetNo() + JsonUtil.toText(analysisContext.readRowHolder()
                                                                                                                                                                                                                       .getCurrentRowAnalysisResult())),ZoneInfo.class);
                                // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样。这里注意DemoData.class 可以每次都变，我这里为了方便 所以用的同一个class 实际上可以一直变
                                WriteSheet writeSheet = EasyExcel.writerSheet(1,"乡镇街道行政区划迁移").head(ZoneInfo.class)
                                                                 .build();
                                // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
                                excelWriter.write(resultSheetInfos,writeSheet);
                            }
                            if(Constants.Number.NUM_ONE.equals(sheetNo1)) {
                                List<ZoneInfo> resultSheetInfos = JsonUtil.toList((String)stringRedisTemplate.opsForHash()
                                                                                                             .get("area_merge",dateformat + uuid + ":" + analysisContext1.getCurrentSheet()
                                                                                                                                                                         .getSheetNo() + JsonUtil.toText(analysisContext1.readRowHolder()
                                                                                                                                                                                                                         .getCurrentRowAnalysisResult())),ZoneInfo.class);
                                // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样。这里注意DemoData.class 可以每次都变，我这里为了方便 所以用的同一个class 实际上可以一直变
                                WriteSheet writeSheet = EasyExcel.writerSheet(2,"区县行政区划迁移").head(ZoneInfo.class)
                                                                 .build();
                                // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
                                excelWriter.write(resultSheetInfos,writeSheet);
                            }
                        } catch(Exception e) {
                            e.printStackTrace();
                        } finally {
                            // 千万别忘记finish 会帮忙关闭流
                            if(excelWriter != null) {
                                excelWriter.finish();
                            }
                        }
                    }
                }
            }
        }
        // 1.5 从/opt/delete目录下读取所有excel文档,读取所需要删除的区划进行逐行解析
        List<File> deleteFile = FileUtil.getAllFile(deleteFilePath);
        if(null != deleteFile) {
            for(File file : deleteFile) {
                try {
                    fileName = file.getName();
                    // 1.3.1 获取合并的两方街道信息,以及合并后的名称
                    // 省名称,地市名称,区县名称,街道名称,省名称,地市名称,区县名称,街道名称,合并后的名称
                    byte[] fileByte = FileUtil.getFileByte(file);
                    inputStream = new ByteArrayInputStream(fileByte);
                    Sheet sheet = new Sheet(1,1,ZoneInfo.class);
                    //开始读取
                    ExcelReader excelReader = new ExcelReader(inputStream,ExcelTypeEnum.XLSX,null,listener);
                    excelReader.read(sheet);
                    analysisContext = excelReader.analysisContext();
                    byte[] fileByte2 = FileUtil.getFileByte(file);
                    inputStream = new ByteArrayInputStream(fileByte2);
                    Sheet sheet2 = new Sheet(2,1,ZoneInfo.class);
                    //开始读取
                    ExcelReader excelReader1 = new ExcelReader(inputStream,ExcelTypeEnum.XLSX,null,listener);
                    excelReader1.read(sheet2);
                    analysisContext1 = excelReader1.analysisContext();
                } catch(IOException e) {
                    log.debug("发生异常:{}",e);
                } finally {
                    // 执行完毕后将file 删除
                    if(file.exists()) {
                        // 将文件移动到bak目录下
                        FileUtil.copyFile(file.getPath(),deleteFilePath + File.separator + "bak",dateformat + uuid + file.getName());
                        file.delete();
                    }
                    // 从redis中获取excel执行数据,将信息写回到excel中
                    if(stringRedisTemplate.hasKey("area_merge")) {
                        ExcelWriter excelWriter = null;
                        try {
                            // 这里 指定文件
                            excelWriter = EasyExcel.write(deleteFilePath + File.separator + "bak" + File.separator + fileName)
                                                   .build();
                            Integer sheetNo = analysisContext.readSheetHolder().getSheetNo();
                            Integer sheetNo1 = analysisContext1.readSheetHolder().getSheetNo();
                            if(Constants.Number.NUM_ZERO.equals(sheetNo)) {
                                List<ZoneInfo> resultSheetInfos = JsonUtil.toList((String)stringRedisTemplate.opsForHash()
                                                                                                             .get("area_merge",dateformat + uuid + ":" + analysisContext.getCurrentSheet()
                                                                                                                                                                        .getSheetNo() + JsonUtil.toText(analysisContext.readRowHolder()
                                                                                                                                                                                                                       .getCurrentRowAnalysisResult())),ZoneInfo.class);
                                // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样。这里注意DemoData.class 可以每次都变，我这里为了方便 所以用的同一个class 实际上可以一直变
                                WriteSheet writeSheet = EasyExcel.writerSheet(1,"删除乡镇街道行政区划").head(ZoneInfo.class)
                                                                 .build();
                                // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
                                excelWriter.write(resultSheetInfos,writeSheet);
                            }
                            if(Constants.Number.NUM_ONE.equals(sheetNo1)) {
                                List<ZoneInfo> resultSheetInfos = JsonUtil.toList((String)stringRedisTemplate.opsForHash()
                                                                                                             .get("area_merge",dateformat + uuid + ":" + analysisContext1.getCurrentSheet()
                                                                                                                                                                         .getSheetNo() + JsonUtil.toText(analysisContext1.readRowHolder()
                                                                                                                                                                                                                         .getCurrentRowAnalysisResult())),ZoneInfo.class);
                                // 每次都要创建writeSheet 这里注意必须指定sheetNo 而且sheetName必须不一样。这里注意DemoData.class 可以每次都变，我这里为了方便 所以用的同一个class 实际上可以一直变
                                WriteSheet writeSheet = EasyExcel.writerSheet(2,"删除区县行政区划").head(ZoneInfo.class)
                                                                 .build();
                                // 分页去数据库查询数据 这里可以去数据库查询每一页的数据
                                excelWriter.write(resultSheetInfos,writeSheet);
                            }
                        } catch(Exception e) {
                            e.printStackTrace();
                        } finally {
                            // 千万别忘记finish 会帮忙关闭流
                            if(excelWriter != null) {
                                excelWriter.finish();
                            }
                        }
                    }
                }
            }
        }
        try {
            if(null != inputStream) {
                inputStream.close();
            }
        } catch(IOException e) {
            throw new RuntimeException(e);
        }
        // 最后执行area_code_full表
        String areaDelSql = "DELETE FROM area_code_full WHERE 1=1";
        String areaInsertSql = "INSERT INTO area_code_full (SELECT tc1.code_id AS sheng_code,tc2.code_id AS dishi_code,tc3.code_id AS quxian_code,tc4.code_id AS jiedao_code,tc1.code_name AS sheng_name,tc2.code_name AS dishi_name,tc3.code_name AS quxian_name,tc4.code_name AS jiedao_name,CONCAT( tc1.code_name, tc2.code_name, tc3.code_name, tc4.code_name ) AS fullname FROM tbsys_code tc1,tbsys_code tc2,tbsys_code tc3,tbsys_code tc4 WHERE tc1.code_id = tc2.pcode_id AND tc2.code_id = tc3.pcode_id AND tc3.code_id = tc4.pcode_id AND tc1.code_type = '2_1' AND LENGTH( tc2.code_id )> 4 ORDER BY tc1.code_id)";
        String areaInsertSql1 = "INSERT INTO area_code_full (SELECT tc1.code_id AS sheng_code,tc2.code_id AS dishi_code,tc3.code_id AS quxian_code,'' AS jiedao_code,tc1.code_name AS sheng_name,tc2.code_name AS dishi_name,tc3.code_name AS quxian_name,'' AS jiedao_name,CONCAT( tc1.code_name, tc2.code_name, tc3.code_name) AS fullname FROM tbsys_code tc1,tbsys_code tc2,tbsys_code tc3,tbsys_code tc4 WHERE tc1.code_id = tc2.pcode_id AND tc2.code_id = tc3.pcode_id AND tc3.code_id = tc4.pcode_id AND tc1.code_type = '2_1' AND LENGTH( tc2.code_id )> 4 ORDER BY tc1.code_id)";
        jdbcTemplate.update(areaDelSql);
        jdbcTemplate.update(areaInsertSql);
        jdbcTemplate.update(areaInsertSql1);
        // 清理tbsys_user缓存
        String sql1 = "DELETE FROM tbsys_department_child_cache WHERE 1=1";
        String sql2 = "DELETE FROM tbsys_user_child_cache WHERE 1=1";
        String sql3 = "INSERT INTO tbsys_department_child_cache (id,childid) (SELECT id,depart_getChilds_copy1(id) FROM tbsys_department)";
        String sql4 = "INSERT INTO tbsys_user_child_cache (id,childid) (SELECT id, depart_getChilds_copy1 (id) AS childid FROM tbsys_department)";
        String sql5 = "INSERT INTO tbsys_department_child_cache(`id`, `childid`) VALUES ('-1', '$,100000,200000')";
        String sql6 = "INSERT INTO tbsys_user_child_cache (id,childid) VALUES ('-1','$,100000,200000')";
        String sql7 = "UPDATE tbsys_department_child_cache SET childid=CONCAT(childid,',')";
        String sql8 = "UPDATE tbsys_user_child_cache SET childid=CONCAT(childid,',')";
        jdbcTemplate.batchUpdate(sql1,sql2,sql3,sql4,sql5,sql6,sql7,sql8);
    }

    /**
     * 新增行政区划
     * @param zoneInfo zoneInfo
     */
    @DS("db2")
    @Transactional(transactionManager = "db2TransactionManager", rollbackFor = Exception.class)
    public void toSaveXzds(ZoneInfo zoneInfo) {
        // 2. 执行新增描述
        // 2.1 新增字典(tbsys_code表
        // insert into tbsys_code value('1',)
        // 2.2 新增用户(tbsys_user,tbsys_department,auth_user表
        /**
         * 描述:nativeTwoName --> nativeCityName
         *     nativeOneName --> nativeProName
         */
        String level;
        String maxDeptId;
        // 字符串拼接的部门名称,用户生成账号
        String concatAccount = null;
        Map<String,Object> codeMap = null;
        Date date = new Date();
        // 创建tbsys_code
        SystemCode systemCode = new SystemCode();
        // 新增用户
        SystemUser systemUser = new SystemUser();
        // 创建tbsys_department
        Department department = new Department();
        String optionName = zoneInfo.getOptionName();
        String nativeOneName = zoneInfo.getNativeOneName();
        String nativeTwoName = zoneInfo.getNativeTwoName();
        String nativeThreeName = zoneInfo.getNativeThreeName();
        String nativeFourName = zoneInfo.getNativeFourName();
        try {
            if(Constants.ZHCN.XZDS.equals(optionName)) {
                if(StringUtils.isNotBlank(nativeOneName) && StringUtils.isNotBlank(nativeTwoName)) {
                    try {
                        // 执行任务:新增地市时确认地市是否存在
                        codeMap = jdbcTemplate.queryForMap("  SELECT b.code_id as codeId,a.code_id as proId FROM tbsys_code a LEFT JOIN tbsys_code b ON a.code_id = b.pcode_id \n" + "  WHERE a.code_type = '2_1' AND a.code_name = \'" + nativeOneName + "\' AND b.code_name = \'" + nativeTwoName + "\'");
                        if(null != codeMap) {
                            log.info("任务:新增地市,地市已存在:{},省:{},市:{}",nativeOneName,nativeTwoName);
                            return;
                        }
                    } catch(DataAccessException e) {
                        // 地市不存在会出异常
                        log.info("任务:新增地市,地市不存在,进行下一步的执行:{},省:{},市:{}",e,nativeOneName,nativeTwoName);
                    }
                    try {
                        codeMap = jdbcTemplate.queryForMap("select code_id as codeId,code_id as proId from tbsys_code where code_name = \'" + nativeOneName + "\' and code_type = '2_1'");
                    } catch(DataAccessException e) {
                        zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                        log.info("任务:新增地市,数据库中未查询到信息:{},省:{},市:{}",e,nativeOneName,nativeTwoName);
                    }
                } else{
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                }
            } else if(Constants.ZHCN.XZQX.equals(optionName)) {
                if(StringUtils.isNotBlank(nativeOneName) && StringUtils.isNotBlank(nativeTwoName) && StringUtils.isNotBlank(nativeThreeName)) {
                    try {
                        codeMap = jdbcTemplate.queryForMap("  SELECT c.code_id as codeId,a.code_id as proId FROM tbsys_code a LEFT JOIN tbsys_code b ON a.code_id = b.pcode_id LEFT JOIN tbsys_code c ON b.code_id = c.pcode_id  WHERE a.code_type = '2_1' AND a.code_name = \'" + nativeOneName + "\' AND b.code_name = \'" + nativeTwoName + "\' AND c.code_name = \'" + nativeThreeName + "\';");
                    } catch(DataAccessException e) {
                        zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                        log.info("任务:新增区/县,区/县不存在,进行下一步的执行:{},省:{},市:{},区:{}",e,nativeOneName,nativeTwoName,nativeThreeName);
                    }
                    try {
                        codeMap = jdbcTemplate.queryForMap("  SELECT b.code_id as codeId,a.code_id as proId FROM tbsys_code a LEFT JOIN tbsys_code b ON a.code_id = b.pcode_id \n" + "  WHERE a.code_type = '2_1' AND a.code_name = \'" + nativeOneName + "\' AND b.code_name = \'" + nativeTwoName + "\'");
                    } catch(DataAccessException e) {
                        zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                        log.info("任务:新增区/县,数据库中未查询到信息:{},省:{},市:{},区:{}",e,nativeOneName,nativeTwoName,nativeThreeName);
                    }
                } else {
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                }
            } else if(Constants.ZHCN.XZJD.equals(optionName)) {
                if(StringUtils.isNotBlank(nativeOneName) && StringUtils.isNotBlank(nativeTwoName) && StringUtils.isNotBlank(nativeThreeName) && StringUtils.isNotBlank(nativeFourName)) {
                    try {
                        codeMap = jdbcTemplate.queryForMap("SELECT d.code_id as codeId,c.code_id as pCodeId,c.code_name as pCodeName,,a.code_id as proId FROM tbsys_code a LEFT JOIN tbsys_code b ON a.code_id = b.pcode_id LEFT JOIN tbsys_code c ON b.code_id = c.pcode_id LEFT JOIN tbsys_code d ON c.code_id = d.pcode_id WHERE a.code_type = '2_1' AND a.code_name =  \'" + nativeOneName + "\'  AND b.code_name =  \'" + nativeTwoName + "\' AND c.code_name = \'" + nativeThreeName + "\' and d.code_name = \'" + nativeFourName + "\'");
                    } catch(DataAccessException e) {
                        log.info("任务:新增乡镇/街道,街道/乡镇未重复执行下一步操作:{},省:{},市:{},区:{},街道:{}",e,nativeOneName,nativeTwoName,nativeThreeName,nativeFourName);
                    }
                    try {
                        codeMap = jdbcTemplate.queryForMap("  SELECT c.code_id as codeId,a.code_id as proId FROM tbsys_code a LEFT JOIN tbsys_code b ON a.code_id = b.pcode_id LEFT JOIN tbsys_code c ON b.code_id = c.pcode_id  WHERE a.code_type = '2_1' AND a.code_name = \'" + nativeOneName + "\' AND b.code_name = \'" + nativeTwoName + "\' AND c.code_name = \'" + nativeThreeName + "\';");
                    } catch(DataAccessException e) {
                        zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                        log.info("任务:新增乡镇/街道,数据库中未查询到信息:{},省:{},市:{},区:{}",e,nativeOneName,nativeTwoName,nativeThreeName);
                    }
                } else {
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                }
            }
            if(null != codeMap && !codeMap.isEmpty()) {
                String proId = (String)codeMap.get("proId");
                // 只有本省的才会去处理
                if(localCode.equals(proId)) {
                    String codeId = (String)codeMap.get("codeId");
                    // 新增地市
                    if(Constants.ZHCN.XZDS.equals(optionName)) {
                        level = Constants.OrgLevel.LEVEL_DSSFJ;
                        maxDeptId = departmentMapper.selectMinDeptId(level,codeId);
                        // 直辖市处理
                        if(Constants.XzqhCode.BEIJING.equals(codeId) || Constants.XzqhCode.CHONGQING.equals(codeId) || Constants.XzqhCode.SHANGHAI.equals(codeId) || Constants.XzqhCode.TIANJIN.equals(codeId)) {
                            if(Constants.OrgLevel.LEVEL_QXSFJ.equals(level)) {
                                //直辖市区县司法局-->查询直辖市下所有区县司法局最大部门编号+1
                                int i;
                                if(null == maxDeptId || maxDeptId.isEmpty()) {
                                    i = Integer.parseInt(codeId) + 1;
                                } else {
                                    i = Integer.parseInt(maxDeptId) + 1;
                                }
                                int noExistsId = getNoExistsId(i,1);
                                department.setId(String.valueOf(noExistsId));
                            }
                        } else {
                            if(Constants.OrgLevel.LEVEL_DSSFJ.equals(level)) {
                                //省地市司法局-->查询省份下所有地市司法局最大部门编号+100
                                int i;
                                if(null == maxDeptId || maxDeptId.isEmpty()) {
                                    i = Integer.parseInt(codeId) + 100;
                                } else {
                                    i = Integer.parseInt(maxDeptId) + 100;
                                }
                                int noExistsId = getNoExistsId(i,100);
                                department.setId(String.valueOf(noExistsId));
                            }
                        }
                        department.setName(nativeTwoName + "司法局");
                        department.setLevel(Constants.OrgLevel.LEVEL_DSSFJ);
                        concatAccount = nativeOneName + "," + nativeTwoName + "司法局";
                        systemCode.setCodeName(nativeTwoName);
                        systemUser.setPosition(Constants.Position.POSITION_ID_DSSFJ);
                        systemUser.setRoleid(Constants.RoleId.ROLE_ID_DSSFJ);
                        // 新增区划
                    } else if(Constants.ZHCN.XZQX.equals(optionName)) {
                        level = Constants.OrgLevel.LEVEL_DSSFJ;
                        maxDeptId = departmentMapper.selectMinDeptId(level,codeId);
                        int i;
                        if(null == maxDeptId || maxDeptId.isEmpty()) {
                            i = Integer.parseInt(codeId) + 1;
                        } else {
                            i = Integer.parseInt(maxDeptId) + 1;
                        }
                        int noExistsId = getNoExistsId(i,1);
                        department.setId(String.valueOf(noExistsId));
                        department.setName(nativeThreeName + "司法局");
                        department.setLevel(Constants.OrgLevel.LEVEL_QXSFJ);
                        concatAccount = nativeOneName + "," + nativeTwoName + "," + nativeThreeName + "司法局";
                        systemCode.setCodeName(nativeThreeName);
                        systemUser.setPosition(Constants.Position.POSITION_ID_QXSFJ);
                        systemUser.setRoleid(Constants.RoleId.ROLE_ID_QXSFJ);
                    } else if(Constants.ZHCN.XZJD.equals(optionName)) {
                        level = Constants.OrgLevel.LEVEL_SFS;
                        maxDeptId = departmentMapper.selectMaxDeptIdInce(codeId,level);
                        if(null == maxDeptId || maxDeptId.isEmpty()) {
                            department.setId(codeId + "001");
                        } else {
                            department.setId(maxDeptId);
                        }
                        department.setName(nativeFourName + "司法所");
                        department.setLevel(Constants.OrgLevel.LEVEL_SFS);
                        concatAccount = nativeOneName + "," + nativeTwoName + "," + nativeThreeName + "," + nativeFourName + "司法所";
                        systemCode.setCodeName(nativeFourName);
                        systemUser.setPosition(Constants.Position.POSITION_ID_SFS);
                        systemUser.setRoleid(Constants.RoleId.ROLE_ID_SFS);
                    }
                    // 上级编号
                    department.setPid(codeId);
                    department.setStat(Constants.Number.NUM_ONE);
                    departmentMapper.insertDepartment(department);
                    systemCode.setCodeType(Constants.CodeType.CODE_ZHRMGHG_XZQH_DM);
                    systemCode.setCodeTypeName("中华人民共和国行政区划代码");
                    systemCode.setCodeId(department.getId());
                    systemCode.setPcodeId(department.getPid());
                    systemCode.setCodeDescribe("无");
                    systemCode.setCreateUserId("sys");
                    systemCode.setCreateTime(date);
                    systemCode.setSn(null == systemCodeMapper.getSystemCodeMaxSn() ? 1 : systemCodeMapper.getSystemCodeMaxSn());
                    systemCodeMapper.insertSystemCode(systemCode);
                    // 账号生成规则:河南省郑州市(hns_zzssfj)
                    String finalConcatAccount = concatAccount;
                    String account = jdbcTemplate.execute((CallableStatementCreator)conn -> {
                        final String callProcedureSql = "{call create_account(?,?)}";
                        CallableStatement prepareCall = conn.prepareCall(callProcedureSql);
                        prepareCall.setString(1,finalConcatAccount);
                        prepareCall.registerOutParameter(2,Types.VARCHAR);
                        return prepareCall;
                    },call -> {
                        call.execute();
                        String string = call.getString(2);
                        return string;
                    });
                    // 查询账户是否重复
                    String account1 = getNoExistsAccount(account,Constants.Number.NUM_ONE);
                    // 生成账号
                    systemUser.setAccount(account1);
                    systemUser.setDepartid(department.getId());
                    systemUser.setUsername(department.getName() + "管理人员");
                    systemUser.setOrgid(department.getPid());
                    systemUser.setStat(Constants.Number.NUM_ONE);
                    systemUserMapper.insertStstemUser(systemUser);
                    // 创建auth_user
                    AuthUser authUser = new AuthUser();
                    authUser.setAccount(account1);
                    authUser.setPassword(password);
                    authUser.setStat(Constants.Number.NUM_ONE);
                    authUserMapper.insertAuthUser(authUser);
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CLCG);
                } else {
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                }
            } else {
                zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
            }
        } catch(Exception e) {
            zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
            if(Constants.ZHCN.XZDS.equals(optionName)) {
                log.debug("任务:新增地市,发生异常:{},事务回滚",e);
            } else if(Constants.ZHCN.XZQX.equals(optionName)) {
                log.debug("任务:新增区/县,发生异常:{},事务回滚",e);
            } else if(Constants.ZHCN.XZJD.equals(optionName)) {
                log.debug("任务:新增乡镇/街道,发生异常:{},事务回滚",e);
            }
            throw new RuntimeException(e);
        }
    }

    /**
     * 生成account的账号保留
     * 生成数据库不重复的account
     * @param account 账号
     * @param numStr 自增步长
     * @return String确认数据库中不重复的账号
     */
    private String getNoExistsAccount(String account,Integer numStr) {
        Map<String,String> paramMap = new HashMap<>(16);
        //判断i是否存在
        boolean flag = true;
        while(flag) {
            paramMap.put("account",account);
            SystemUser systemUser = systemUserMapper.selectUserByParam(paramMap);
            if(null != systemUser) {
                account = systemUser.getAccount() + numStr;
            } else {
                flag = false;
            }
        }
        return account;
    }

    /**
     * 生成数据库不重复的最大部门ID
     * @param id 原始id
     * @param step 自增步数
     * @return int int
     */
    private int getNoExistsId(int id,int step) {
        //判断i是否存在
        boolean flag = true;
        while(flag) {
            Department department = departmentMapper.selectByDeptId(String.valueOf(id));
            if(null != department) {
                id += step;
            } else {
                flag = false;
            }
        }
        return id;
    }

    /**
     * 更新行政区划:修改地市名称
     * @param zoneInfo zoneInfo
     * @param mergeUuid mergeUuid
     */
    @DS("db2")
    @Transactional(transactionManager = "db2TransactionManager", rollbackFor = Exception.class)
    public void toUpdateXgds(ZoneInfo zoneInfo,String mergeUuid) {
        Map<String,Object> codeMap = null;
        String nativeOneName = zoneInfo.getNativeOneName();
        String nativeTwoName = zoneInfo.getNativeTwoName();
        String nativeThreeName = zoneInfo.getNativeThreeName();
        /**
         * 描述: nativeOneName --> nativeProName
         *      nativeTwoName --> nativeCityName
         *      nativeThreeName --> newCityName
         */
        try {
            if(StringUtils.isNotBlank(nativeThreeName)) {
                try {
                    codeMap = jdbcTemplate.queryForMap("SELECT a.code_id as pCodeId,b.code_id as codeId,a.code_id as proId FROM tbsys_code a LEFT JOIN tbsys_code b ON a.code_id = b.pcode_id \n" + "  WHERE a.code_type = '2_1' AND a.code_name = \'" + nativeOneName + "\' AND b.code_name = \'" + nativeTwoName + "\'");
                } catch(DataAccessException e) {
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                    log.info("任务:修改地市名称,数据库中未查询到信息:{},省:{},市:{}",e,nativeOneName,nativeTwoName);
                }
                // 判断codeMap是否存在,不存在则返回
                if(null != codeMap && !codeMap.isEmpty()) {
                    String proId = (String)codeMap.get("proId");
                    if(localCode.equals(proId)) {
                        String codeId = (String)codeMap.get("codeId");
                        String pCodeId = (String)codeMap.get("pCodeId");
                        String mergeJson = JsonUtil.toText(zoneInfo);
                        // 反射获取执行方法
                        Class<ZoningMapper> zoningMapperClass = ZoningMapper.class;
                        Method[] methods = zoningMapperClass.getMethods();
                        List<Method> updateCityMethods = new ArrayList<>();
                        List<Method> selectCityMethods = new ArrayList<>();
                        for(Method method : methods) {
                            String name = method.getName();
                            if(name.contains("updateCityName")) {
                                updateCityMethods.add(method);
                            } else if(name.contains("selectCity")) {
                                selectCityMethods.add(method);
                            }
                        }
                        // 执行查询方法,将查询数据库的结果缓存到area_merge_log表中
                        for(Method selectMethod : selectCityMethods) {
                            // 获取方法的返回值
                            List<Map<String,Object>> invoke = (List<Map<String,Object>>)selectMethod.invoke(zoningMapper,codeId,null,null);
                            if(!CollectionUtils.isEmpty(invoke)) {
                                for(Map<String,Object> stringObjectMap : invoke) {
                                    // 向area_merge_log表中插入日志信息
                                    zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId,Constants.NumberStr.NUM_STR_ONE,mergeUuid);
                                }
                            }
                        }
                        // 执行修改地市名称方法
                        for(Method doMethod : updateCityMethods) {
                            // 调用方法执行
                            doMethod.invoke(zoningMapper,nativeThreeName,codeId,codeId,null,null);
                        }
                        Map<String,String> systemMap = new HashMap<>(16);
                        // 更改数据前先将数据进行备份
                        List<Map<String,Object>> maps = systemCodeMapper.selectSystemCodeByCodeId(codeId,null);
                        if(!CollectionUtils.isEmpty(maps)) {
                            for(Map<String,Object> stringObjectMap : maps) {
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId,Constants.NumberStr.NUM_STR_ONE,mergeUuid);
                            }
                        }
                        SystemCode systemCode = new SystemCode();
                        systemCode.setCodeName(nativeThreeName);
                        systemMap.put("codeId",codeId);
                        systemMap.put("pcodeId",pCodeId);
                        systemCodeMapper.updateSystemCode(systemCode,systemMap);
                        List<Map<String,Object>> departmentMap = departmentMapper.selectDepartmentById(codeId,null);
                        if(!CollectionUtils.isEmpty(departmentMap)) {
                            for(Map<String,Object> stringObjectMap : departmentMap) {
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId,Constants.NumberStr.NUM_STR_ONE,mergeUuid);
                            }
                        }
                        Department department1 = new Department();
                        department1.setName(nativeThreeName + "司法局");
                        HashMap<String,String> paramMap = new HashMap<>(16);
                        paramMap.put("id",codeId);
                        departmentMapper.updateDepartmentById(department1,paramMap);
                        List<Map<String,Object>> systemUserMap = systemUserMapper.selectSystemUserByDepartmentId(codeId,null);
                        if(!CollectionUtils.isEmpty(systemUserMap)) {
                            for(Map<String,Object> stringObjectMap : systemUserMap) {
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId,Constants.NumberStr.NUM_STR_ONE,mergeUuid);
                            }
                        }
                        SystemUser systemUser = new SystemUser();
                        systemUser.setUsername(nativeThreeName + "管理人员");
                        Map<String,String> userMap = new HashMap<>(16);
                        userMap.put("departid",codeId);
                        systemUserMapper.updateSystemUser(systemUser,userMap);
                        zoneInfo.setResultOneMessage(Constants.ZHCN.CLCG);
                    } else {
                        zoneInfo.setResultOneMessage(Constants.ZHCN.CODEERROR);
                    }
                } else {
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                }
            } else {
                zoneInfo.setResultOneMessage(Constants.ZHCN.CXSB);
            }
        } catch(Exception e) {
            zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
            log.debug("任务:修改地市名称,发生异常:{},事务回滚",e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 行政区划修改:修改区县名称
     * @param zoneInfo zoneInfo
     * @param mergeUuid mergeUuid
     */
    @DS("db2")
    @Transactional(transactionManager = "db2TransactionManager", rollbackFor = Exception.class)
    public void toUpdateXgqx(ZoneInfo zoneInfo,String mergeUuid) {
        Map<String,Object> codeMap = null;
        /**
         * 描述: nativeOneName --> nativeProName
         *      nativeTwoName --> nativeCityName
         *      nativeThreeName --> nativeCouName
         *      nativeFourName --> newCouName
         */
        String nativeOneName = zoneInfo.getNativeOneName();
        String nativeTwoName = zoneInfo.getNativeTwoName();
        String nativeThreeName = zoneInfo.getNativeThreeName();
        String nativeFourName = zoneInfo.getNativeFourName();
        try {
            if (StringUtils.isNotBlank(nativeFourName)) {
                try {
                    codeMap = jdbcTemplate.queryForMap("SELECT c.code_id as codeId,b.code_id as pCodeId,a.code_id as proId FROM tbsys_code a LEFT JOIN tbsys_code b ON a.code_id = b.pcode_id LEFT JOIN tbsys_code c ON b.code_id = c.pcode_id  WHERE a.code_type = '2_1' AND a.code_name = \'" + nativeOneName + "\' AND b.code_name = \'" + nativeTwoName + "\' AND c.code_name = \'" + nativeThreeName + "\';");
                } catch (DataAccessException e) {
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                    log.info("任务:修改区/县名称,数据库中未查询到信息:{},省:{},市:{},区:{}", e, nativeOneName, nativeTwoName, nativeThreeName);
                }
                // 判断codeMap是否存在,不存在则返回
                if (null != codeMap && !codeMap.isEmpty()) {
                    String proId = (String) codeMap.get("proId");
                    if (localCode.equals(proId)) {
                        String codeId = (String) codeMap.get("codeId");
                        String pCodeId = (String) codeMap.get("pCodeId");
                        String mergeJson = JsonUtil.toText(zoneInfo);
                        // 反射获取执行方法
                        Class<ZoningMapper> zoningMapperClass = ZoningMapper.class;
                        Method[] methods = zoningMapperClass.getMethods();
                        List<Method> updateCountyMethods = new ArrayList<>();
                        List<Method> selectCountyMethods = new ArrayList<>();
                        for (Method method : methods) {
                            String name = method.getName();
                            if (name.contains("mergeCounty")) {
                                updateCountyMethods.add(method);
                            } else if (name.contains("selectCounty")) {
                                selectCountyMethods.add(method);
                            }
                        }
                        // 执行查询方法,将查询数据库的结果缓存到area_merge_log表中
                        for (Method selectMethod : selectCountyMethods) {
                            List<Map<String, Object>> invoke = (List<Map<String, Object>>) selectMethod.invoke(zoningMapper, codeId, null);
                            if (!CollectionUtils.isEmpty(invoke)) {
                                for (Map<String, Object> stringObjectMap : invoke) {
                                    // 向area_merge_log表中插入日志信息
                                    zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")), String.valueOf(stringObjectMap.get("tableName")), String.valueOf(stringObjectMap.get("field")), JsonUtil.toText(stringObjectMap), mergeJson, codeId, codeId, Constants.NumberStr.NUM_STR_TWO, mergeUuid);
                                }
                            }
                        }
                        for (Method doMethod : updateCountyMethods) {
                            doMethod.invoke(zoningMapper, nativeFourName, codeId, codeId, null);
                        }
                        List<Map<String, Object>> maps = systemCodeMapper.selectSystemCodeByCodeId(codeId, null);
                        if (!CollectionUtils.isEmpty(maps)) {
                            for (Map<String, Object> stringObjectMap : maps) {
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")), String.valueOf(stringObjectMap.get("tableName")), String.valueOf(stringObjectMap.get("field")), JsonUtil.toText(stringObjectMap), mergeJson, codeId, codeId, Constants.NumberStr.NUM_STR_TWO, mergeUuid);
                            }
                        }
                        Map<String, String> systemMap = new HashMap<>(16);
                        systemMap.put("codeId", codeId);
                        systemMap.put("pcodeId", pCodeId);
                        SystemCode systemCode = new SystemCode();
                        systemCode.setCodeName(nativeFourName);
                        systemCodeMapper.updateSystemCode(systemCode, systemMap);
                        List<Map<String, Object>> departmentMap = departmentMapper.selectDepartmentById(codeId, null);
                        if (!CollectionUtils.isEmpty(departmentMap)) {
                            for (Map<String, Object> stringObjectMap : departmentMap) {
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")), String.valueOf(stringObjectMap.get("tableName")), String.valueOf(stringObjectMap.get("field")), JsonUtil.toText(stringObjectMap), mergeJson, codeId, codeId, Constants.NumberStr.NUM_STR_TWO, mergeUuid);
                            }
                        }
                        Department department1 = new Department();
                        department1.setName(nativeFourName + "司法局");
                        HashMap<String, String> paramMap = new HashMap<>(16);
                        paramMap.put("id", codeId);
                        departmentMapper.updateDepartmentById(department1, paramMap);
                        List<Map<String, Object>> systemUserMap = systemUserMapper.selectSystemUserByDepartmentId(codeId, null);
                        if (!CollectionUtils.isEmpty(systemUserMap)) {
                            for (Map<String, Object> stringObjectMap : systemUserMap) {
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")), String.valueOf(stringObjectMap.get("tableName")), String.valueOf(stringObjectMap.get("field")), JsonUtil.toText(stringObjectMap), mergeJson, codeId, codeId, Constants.NumberStr.NUM_STR_TWO, mergeUuid);
                            }
                        }
                        SystemUser systemUser = new SystemUser();
                        systemUser.setUsername(nativeFourName + "管理人员");
                        Map<String, String> userMap = new HashMap<>(16);
                        userMap.put("departid", codeId);
                        systemUserMapper.updateSystemUser(systemUser, userMap);
                        zoneInfo.setResultOneMessage(Constants.ZHCN.CLCG);
                    } else {
                        zoneInfo.setResultOneMessage(Constants.ZHCN.CODEERROR);
                    }
                } else {
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CXSB);
                }
            } else {
                zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
            }
        } catch(Exception e) {
            zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
            log.debug("任务:修改区/县名称,发生异常:{},事务回滚",e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 行政区划修改:修改街道名称
     * @param zoneInfo zoneInfo
     * @param mergeUuid mergeUuid
     */
    @DS("db2")
    @Transactional(transactionManager = "db2TransactionManager", rollbackFor = Exception.class)
    public void toUpdateXgjd(ZoneInfo zoneInfo,String mergeUuid) {
        Map<String,Object> codeMap = null;
        /**
         * 描述: nativeOneName --> nativeProName
         *      nativeTwoName --> nativeCityName
         *      nativeThreeName --> nativeCouName
         *      nativeFourName --> nativeVillName
         *      nativeFiveName --> newVillName
         */
        String nativeOneName = zoneInfo.getNativeOneName();
        String nativeTwoName = zoneInfo.getNativeTwoName();
        String nativeThreeName = zoneInfo.getNativeThreeName();
        String nativeFourName = zoneInfo.getNativeFourName();
        String nativeFiveName = zoneInfo.getNativeFiveName();
        try {
            if (StringUtils.isNotBlank(nativeFiveName)) {
                try {
                    codeMap = jdbcTemplate.queryForMap("SELECT d.code_id as codeId,c.code_id as pCodeId,c.code_name as pCodeName,a.code_id as proId FROM tbsys_code a LEFT JOIN tbsys_code b ON a.code_id = b.pcode_id LEFT JOIN tbsys_code c ON b.code_id = c.pcode_id LEFT JOIN tbsys_code d ON c.code_id = d.pcode_id WHERE a.code_type = '2_1' AND a.code_name =  \'" + nativeOneName + "\'  AND b.code_name =  \'" + nativeTwoName + "\' AND c.code_name = \'" + nativeThreeName + "\' and d.code_name = \'" + nativeFourName + "\'");
                } catch (DataAccessException e) {
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                    log.info("任务:修改街道/乡镇名称,数据库中未查询到信息:{},省:{},市:{},区:{},街道:{}", e, nativeOneName, nativeTwoName, nativeThreeName, nativeFourName);
                }
                // 判断codeMap是否存在,不存在则返回
                if (null != codeMap && !codeMap.isEmpty()) {
                    String proId = (String) codeMap.get("proId");
                    if (localCode.equals(proId)) {
                        String codeId = (String) codeMap.get("codeId");
                        String pCodeId = (String) codeMap.get("pCodeId");
                        String pCodeName = (String) codeMap.get("pCodeName");
                        String mergeJson = JsonUtil.toText(zoneInfo);
                        // 反射获取执行方法
                        Class<ZoningMapper> zoningMapperClass = ZoningMapper.class;
                        Method[] methods = zoningMapperClass.getMethods();
                        List<Method> updateVillMethods = new ArrayList<>();
                        List<Method> selectVillMethods = new ArrayList<>();
                        for (Method method : methods) {
                            String name = method.getName();
                            if (name.contains("mergeVill")) {
                                updateVillMethods.add(method);
                            } else if (name.contains("selectVill")) {
                                selectVillMethods.add(method);
                            }
                        }
                        // 执行查询方法,将查询数据库的结果缓存到area_merge_log表中
                        for (Method selectMethod : selectVillMethods) {
                            List<Map<String, Object>> invoke = (List<Map<String, Object>>) selectMethod.invoke(zoningMapper, codeId);
                            if (!CollectionUtils.isEmpty(invoke)) {
                                for (Map<String, Object> stringObjectMap : invoke) {
                                    // 向area_merge_log表中插入日志信息
                                    zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")), String.valueOf(stringObjectMap.get("tableName")), String.valueOf(stringObjectMap.get("field")), JsonUtil.toText(stringObjectMap), mergeJson, codeId, codeId, Constants.NumberStr.NUM_STR_THREE, mergeUuid);
                                }
                            }
                        }
                        for (Method doMethod : updateVillMethods) {
                            doMethod.invoke(zoningMapper, nativeFiveName, codeId, codeId, pCodeName, pCodeId, pCodeId);
                        }
                        List<Map<String, Object>> systemCodeMap = systemCodeMapper.selectSystemCodeByCodeId(codeId, null);
                        if (!CollectionUtils.isEmpty(systemCodeMap)) {
                            for (Map<String, Object> stringObjectMap : systemCodeMap) {
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")), String.valueOf(stringObjectMap.get("tableName")), String.valueOf(stringObjectMap.get("field")), JsonUtil.toText(stringObjectMap), mergeJson, codeId, codeId, Constants.NumberStr.NUM_STR_THREE, mergeUuid);
                            }
                        }
                        Map<String, String> systemMap = new HashMap<>(16);
                        systemMap.put("codeId", codeId);
                        systemMap.put("pcodeId", pCodeId);
                        SystemCode systemCode = new SystemCode();
                        systemCode.setCodeName(nativeFiveName);
                        systemCodeMapper.updateSystemCode(systemCode, systemMap);
                        List<Map<String, Object>> departmentMap = departmentMapper.selectDepartmentById(codeId, null);
                        if (!CollectionUtils.isEmpty(departmentMap)) {
                            for (Map<String, Object> stringObjectMap : departmentMap) {
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")), String.valueOf(stringObjectMap.get("tableName")), String.valueOf(stringObjectMap.get("field")), JsonUtil.toText(stringObjectMap), mergeJson, codeId, codeId, Constants.NumberStr.NUM_STR_THREE, mergeUuid);
                            }
                        }
                        Department department1 = new Department();
                        department1.setName(nativeFiveName + "司法所");
                        HashMap<String, String> paramMap = new HashMap<>(16);
                        paramMap.put("id", codeId);
                        departmentMapper.updateDepartmentById(department1, paramMap);
                        List<Map<String, Object>> systemUserMap = systemUserMapper.selectSystemUserByDepartmentId(codeId, null);
                        if (!CollectionUtils.isEmpty(systemUserMap)) {
                            for (Map<String, Object> stringObjectMap : systemUserMap) {
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")), String.valueOf(stringObjectMap.get("tableName")), String.valueOf(stringObjectMap.get("field")), JsonUtil.toText(stringObjectMap), mergeJson, codeId, codeId, Constants.NumberStr.NUM_STR_THREE, mergeUuid);
                            }
                        }
                        SystemUser systemUser = new SystemUser();
                        systemUser.setUsername(nativeFiveName + "管理人员");
                        Map<String, String> userMap = new HashMap<>(16);
                        userMap.put("departid", codeId);
                        systemUserMapper.updateSystemUser(systemUser, userMap);
                        zoneInfo.setResultOneMessage(Constants.ZHCN.CLCG);
                    } else {
                        zoneInfo.setResultOneMessage(Constants.ZHCN.CODEERROR);
                    }
                } else {
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CXSB);
                }
            } else {
                zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
            }
        } catch (Exception e) {
            zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
            log.debug("任务:修改乡镇/街道名称,发生异常:{},事务回滚", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 行政区划:区县合并
     * @param zoneInfo zoneInfo
     * @param mergeUuid mergeUuid
     */
    @DS("db2")
    @Transactional(transactionManager = "db2TransactionManager", rollbackFor = Exception.class)
    public void toMergeQx(ZoneInfo zoneInfo,String mergeUuid) {
        Map<String,Object> codeMap = null;
        Map<String,Object> codeMap2 = null;
        /**
         * 描述: nativeOneName --> nativeProName
         *      nativeTwoName --> nativeCityName
         *      nativeThreeName --> nativeCouName
         *      nativeFourName --> mergeToProName
         *      nativeFiveName --> mergeToCityName
         *      nativeSixName --> mergeToCouName
         *      nativeSevenName --> newCouName
         */
        String nativeOneName = zoneInfo.getNativeOneName();
        String nativeTwoName = zoneInfo.getNativeTwoName();
        String nativeThreeName = zoneInfo.getNativeThreeName();
        String nativeFourName = zoneInfo.getNativeFourName();
        String nativeFiveName = zoneInfo.getNativeFiveName();
        String nativeSixName = zoneInfo.getNativeSixName();
        String nativeSevenName = zoneInfo.getNativeSevenName();
        try {
            try {
                codeMap = jdbcTemplate.queryForMap("SELECT c.code_id as codeId,b.code_id as pCodeId,a.code_id as proId FROM tbsys_code a LEFT JOIN tbsys_code b ON a.code_id = b.pcode_id LEFT JOIN tbsys_code c ON b.code_id = c.pcode_id  WHERE a.code_type = '2_1' AND a.code_name = \'" + nativeOneName + "\' AND b.code_name = \'" + nativeTwoName + "\' AND c.code_name = \'" + nativeThreeName + "\';");
            } catch(DataAccessException e) {
                zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                log.info("任务:区/县行政区划合并:发起方,数据库中未查询到信息:{},省:{},市:{},区:{}",e,nativeOneName,nativeTwoName,nativeThreeName);
            }
            // 查找mergeToProName,mergeToCityName,mergeToCouName是否存在
            try {
                codeMap2 = jdbcTemplate.queryForMap("SELECT c.code_id as codeId,b.code_id as pCodeId,a.code_id as proId FROM tbsys_code a LEFT JOIN tbsys_code b ON a.code_id = b.pcode_id LEFT JOIN tbsys_code c ON b.code_id = c.pcode_id  WHERE a.code_type = '2_1' AND a.code_name = \'" + nativeFourName + "\' AND b.code_name = \'" + nativeFiveName + "\' AND c.code_name = \'" + nativeSixName + "\';");
            } catch(DataAccessException e) {
                zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                log.info("任务:区/县行政区划合并:发起方,数据库中未查询到信息:{},省:{},市:{},区:{}",e,nativeFourName,nativeFiveName,nativeSixName);
            }
            // 判断合并前区划与即将合并区划是否存在
            if(null != codeMap && null != codeMap2 && !codeMap.isEmpty() && !codeMap2.isEmpty()) {
                String proId = (String)codeMap.get("proId");
                String proId1 = (String)codeMap2.get("proId");
                if(proId.equals(proId1) && localCode.equals(proId1)) {
                    // 把之前nativeCouName对应code的数据与mergeCouName对应code的数据合并到mergeToCouName中
                    String codeId = (String)codeMap.get("codeId");
                    String codeId2 = (String)codeMap2.get("codeId");
                    String oldCityId = (String)codeMap.get("pCodeId");
                    String newCityId = (String)codeMap2.get("pCodeId");
                    String mergeJson = JsonUtil.toText(zoneInfo);
                    // 反射获取执行方法
                    Class<ZoningMapper> zoningMapperClass = ZoningMapper.class;
                    Method[] methods = zoningMapperClass.getMethods();
                    List<Method> updateCountyMethods = new ArrayList<>();
                    List<Method> selectCountyMethods = new ArrayList<>();
                    List<Method> updateCityMethods = new ArrayList<>();
                    List<Method> selectCityMethods = new ArrayList<>();
                    for(Method method : methods) {
                        String name = method.getName();
                        if(name.contains("mergeCounty")) {
                            updateCountyMethods.add(method);
                        } else if(name.contains("selectCounty")) {
                            selectCountyMethods.add(method);
                        } else if(name.contains("updateCityName")) {
                            updateCityMethods.add(method);
                        } else if(name.contains("selectCity")) {
                            selectCityMethods.add(method);
                        }
                    }
                    for(Method selectMethod : selectCityMethods) {
                        // 获取方法的返回值
                        List<Map<String,Object>> invoke = (List<Map<String,Object>>)selectMethod.invoke(zoningMapper,oldCityId,null,null);
                        if(!CollectionUtils.isEmpty(invoke)) {
                            for(Map<String,Object> stringObjectMap : invoke) {
                                // 向area_merge_log表中插入日志信息
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,oldCityId,newCityId,Constants.NumberStr.NUM_STR_FOUR,mergeUuid);
                            }
                        }
                    }
                    for(Method selectMethod : selectCountyMethods) {
                        // 获取方法的返回值
                        List<Map<String,Object>> invoke = (List<Map<String,Object>>)selectMethod.invoke(zoningMapper,codeId,null);
                        if(!CollectionUtils.isEmpty(invoke)) {
                            for(Map<String,Object> stringObjectMap : invoke) {
                                // 向area_merge_log表中插入日志信息
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId2,Constants.NumberStr.NUM_STR_FOUR,mergeUuid);
                            }
                        }
                    }
                    // 地市改名
                    for(Method updateCityMethod : updateCityMethods) {
                        // 调用方法执行
                        updateCityMethod.invoke(zoningMapper,nativeFiveName,oldCityId,newCityId,null,null);
                    }
                    // 合并区县
                    for(Method doMethod : updateCountyMethods) {
                        // 调用方法执行
                        doMethod.invoke(zoningMapper,nativeSevenName,codeId2,codeId,null);
                    }
                    // 区县合并后,是否需要把nativeCouName对应code的用户与mergeCouName对应code的用户转到mergeToCouName对应code下
                    List<Map<String,Object>> departmentMap = departmentMapper.selectDepartmentById(null,codeId);
                    if(!CollectionUtils.isEmpty(departmentMap)) {
                        for(Map<String,Object> stringObjectMap : departmentMap) {
                            zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId,Constants.NumberStr.NUM_STR_THREE,mergeUuid);
                        }
                    }
                    HashMap<String,String> paramMap = new HashMap<>(16);
                    paramMap.put("pid",codeId);
                    paramMap.put("pcodeId",codeId);
                    paramMap.put("orgId",codeId);
                    Department department1 = new Department();
                    department1.setPid(codeId2);
                    departmentMapper.updateDepartmentById(department1,paramMap);
                    List<Map<String,Object>> systemCodeMap = systemCodeMapper.selectSystemCodeByCodeId(null,codeId);
                    if(!CollectionUtils.isEmpty(systemCodeMap)) {
                        for(Map<String,Object> stringObjectMap : systemCodeMap) {
                            zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId2,Constants.NumberStr.NUM_STR_FOUR,mergeUuid);
                        }
                    }
                    SystemCode systemCode = new SystemCode();
                    systemCode.setPcodeId(codeId2);
                    systemCodeMapper.updateSystemCode(systemCode,paramMap);
                    List<Map<String,Object>> systemUserMap = systemUserMapper.selectSystemUserByDepartmentId(null,codeId);
                    if(!CollectionUtils.isEmpty(systemUserMap)) {
                        for(Map<String,Object> stringObjectMap : systemUserMap) {
                            zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId2,Constants.NumberStr.NUM_STR_FOUR,mergeUuid);
                        }
                    }
                    SystemUser systemUser = new SystemUser();
                    systemUser.setOrgid(codeId2);
                    systemUserMapper.updateSystemUser(systemUser,paramMap);
                    // 删除行政区划
                    List<Map<String,Object>> systemCodeMap1 = systemCodeMapper.selectSystemCodeByCodeId(codeId,null);
                    if(!CollectionUtils.isEmpty(systemCodeMap1)) {
                        for(Map<String,Object> stringObjectMap : systemCodeMap1) {
                            zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId2,Constants.NumberStr.NUM_STR_FIVE,mergeUuid);
                        }
                    }
                    systemCodeMapper.deleteTbsysCodeById(codeId);
                    // 删除部门
                    List<Map<String,Object>> departmentMap1 = departmentMapper.selectDepartmentById(codeId,null);
                    if(!CollectionUtils.isEmpty(departmentMap1)) {
                        for(Map<String,Object> stringObjectMap : departmentMap1) {
                            zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId2,Constants.NumberStr.NUM_STR_FIVE,mergeUuid);
                        }
                    }
                    departmentMapper.deleteById(codeId);
                    // 删除auth_user
                    List<Map<String,Object>> authUserMap1 = authUserMapper.selectAuthUserByCodeId(codeId);
                    if(!CollectionUtils.isEmpty(authUserMap1)) {
                        for(Map<String,Object> stringObjectMap : authUserMap1) {
                            zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId2,Constants.NumberStr.NUM_STR_FIVE,mergeUuid);
                        }
                    }
                    authUserMapper.deleteAccountByDepartId(codeId);
                    // 删除用户
                    List<Map<String,Object>> systemUserMap1 = systemUserMapper.selectSystemUserByDepartmentId(codeId,null);
                    if(!CollectionUtils.isEmpty(systemUserMap1)) {
                        for(Map<String,Object> stringObjectMap : systemUserMap1) {
                            zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId2,Constants.NumberStr.NUM_STR_FIVE,mergeUuid);
                        }
                    }
                    systemUserMapper.deleteAccountByDepartId(codeId);
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CLCG);
                } else {
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CODEERROR);
                }
            } else {
                zoneInfo.setResultOneMessage(Constants.ZHCN.CXSB);
            }
        } catch(Exception e) {
            zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
            log.debug("任务:区/县行政区划合并,发生异常:{},事务回滚",e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 行政区划:街道合并
     * @param zoneInfo zoneInfo
     * @param mergeUuid mergeUuid
     */
    @DS("db2")
    @Transactional(transactionManager = "db2TransactionManager", rollbackFor = Exception.class)
    public void toMergeJd(ZoneInfo zoneInfo,String mergeUuid) {
        Map<String,Object> codeMap = null;
        Map<String,Object> codeMap2 = null;
        /**
         * 描述: nativeOneName --> nativeProName
         *      nativeTwoName --> nativeCityName
         *      nativeThreeName --> nativeCouName
         *      nativeFourName --> nativeVillName
         *      nativeFiveName --> mergeToProName
         *      nativeSixName --> mergeToCityName
         *      nativeSevenName --> mergeToCouName
         *      nativeEightName --> mergeToVillName
         *      nativeNineName --> newVillName
         */
        String nativeOneName = zoneInfo.getNativeOneName();
        String nativeTwoName = zoneInfo.getNativeTwoName();
        String nativeThreeName = zoneInfo.getNativeThreeName();
        String nativeFourName = zoneInfo.getNativeFourName();
        String nativeFiveName = zoneInfo.getNativeFiveName();
        String nativeSixName = zoneInfo.getNativeSixName();
        String nativeSevenName = zoneInfo.getNativeSevenName();
        String nativeEightName = zoneInfo.getNativeEightName();
        String nativeNineName = zoneInfo.getNativeNineName();
        try {
            try {
                codeMap = jdbcTemplate.queryForMap("SELECT d.code_id AS codeId,c.code_id as codeId1,b.code_id as cityId,a.code_id as proId FROM tbsys_code a LEFT JOIN tbsys_code b ON a.code_id = b.pcode_id LEFT JOIN tbsys_code c ON b.code_id = c.pcode_id LEFT JOIN tbsys_code d ON c.code_id = d.pcode_id WHERE a.code_type = '2_1' AND a.code_name = \'" + nativeOneName + "\' AND b.code_name = \'" + nativeTwoName + "\' AND c.code_name = \'" + nativeThreeName + "\' AND d.code_name = \'" + nativeFourName + "\'");
            } catch(DataAccessException e) {
                zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                log.info("任务:乡镇/街道行政区划合并:发起方,数据库中未查询到信息:{},省:{},市:{},区:{},街道:{}",e,nativeOneName,nativeTwoName,nativeThreeName,nativeFourName);
            }
            // 查找mergeToProName,mergeToCityName,mergeToCouName是否存在
            try {
                codeMap2 = jdbcTemplate.queryForMap("SELECT d.code_id AS codeId,c.code_id as codeId1,b.code_name as cityName,b.code_id as cityId,a.code_id as proId FROM tbsys_code a LEFT JOIN tbsys_code b ON a.code_id = b.pcode_id LEFT JOIN tbsys_code c ON b.code_id = c.pcode_id LEFT JOIN tbsys_code d ON c.code_id = d.pcode_id WHERE a.code_type = '2_1' AND a.code_name = \'" + nativeFiveName + "\' AND b.code_name = \'" + nativeSixName + "\' AND c.code_name = \'" + nativeSevenName + "\' AND d.code_name = \'" + nativeEightName + "\'");
            } catch(DataAccessException e) {
                zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                log.info("任务:乡镇/街道行政区划合并:合并方,数据库中未查询到信息:{},省:{},市:{},区:{},街道:{}",e,nativeFiveName,nativeSixName,nativeSevenName,nativeEightName);
            }
            // 判断合并前区划与即将合并区划是否存在
            if(null != codeMap && null != codeMap2 && !codeMap.isEmpty() && !codeMap2.isEmpty()) {
                String proId = (String)codeMap.get("proId");
                String proId1 = (String)codeMap2.get("proId");
                if(proId.equals(proId1) && localCode.equals(proId1)) {
                    // 把之前nativeCouName对应code的数据与mergeCouName对应code的数据合并到mergeToCouName中
                    String codeId = (String)codeMap.get("codeId");
                    String codeId2 = (String)codeMap2.get("codeId");
                    String oldCountyId = (String)codeMap.get("codeId1");
                    String newCountyId = (String)codeMap2.get("codeId1");
                    String oldCityId = (String)codeMap.get("cityId");
                    String newCityId = (String)codeMap2.get("cityId");
                    String mergeJson = JsonUtil.toText(zoneInfo);
                    // 反射获取执行方法
                    Class<ZoningMapper> zoningMapperClass = ZoningMapper.class;
                    Method[] methods = zoningMapperClass.getMethods();
                    List<Method> updateCountyMethods = new ArrayList<>();
                    List<Method> selectCountyMethods = new ArrayList<>();
                    List<Method> updateCityMethods = new ArrayList<>();
                    List<Method> selectCityMethods = new ArrayList<>();
                    List<Method> selectVillMethods = new ArrayList<>();
                    List<Method> updateVillMethods = new ArrayList<>();
                    for(Method method : methods) {
                        String name = method.getName();
                        if(name.contains("mergeCounty")) {
                            updateCountyMethods.add(method);
                        } else if(name.contains("selectCounty")) {
                            selectCountyMethods.add(method);
                        } else if(name.contains("updateCityName")) {
                            updateCityMethods.add(method);
                        } else if(name.contains("selectCity")) {
                            selectCityMethods.add(method);
                        } else if(name.contains("mergeVill")) {
                            updateVillMethods.add(method);
                        } else if(name.contains("selectVill")) {
                            selectVillMethods.add(method);
                        }
                    }
                    for(Method selectMethod : selectCityMethods) {
                        // 获取方法的返回值
                        List<Map<String,Object>> invoke = (List<Map<String,Object>>)selectMethod.invoke(zoningMapper,oldCityId,null,null);
                        if(!CollectionUtils.isEmpty(invoke)) {
                            for(Map<String,Object> stringObjectMap : invoke) {
                                // 向area_merge_log表中插入日志信息
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,oldCityId,newCityId,Constants.NumberStr.NUM_STR_FIVE,mergeUuid);
                            }
                        }
                    }
                    for(Method selectMethod : selectCountyMethods) {
                        // 获取方法的返回值
                        List<Map<String,Object>> invoke = (List<Map<String,Object>>)selectMethod.invoke(zoningMapper,oldCountyId,null);
                        if(!CollectionUtils.isEmpty(invoke)) {
                            for(Map<String,Object> stringObjectMap : invoke) {
                                // 向area_merge_log表中插入日志信息
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,oldCountyId,newCountyId,Constants.NumberStr.NUM_STR_FIVE,mergeUuid);
                            }
                        }
                    }
                    for(Method selectVillMethod : selectVillMethods) {
                        // 获取方法的返回值
                        List<Map<String,Object>> invoke = (List<Map<String,Object>>)selectVillMethod.invoke(zoningMapper,codeId);
                        if(!CollectionUtils.isEmpty(invoke)) {
                            for(Map<String,Object> stringObjectMap : invoke) {
                                // 向area_merge_log表中插入日志信息
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId2,Constants.NumberStr.NUM_STR_FIVE,mergeUuid);
                            }
                        }
                    }
                    for(Method updateCityMethod : updateCityMethods) {
                        // 调用方法执行
                        updateCityMethod.invoke(zoningMapper,nativeSixName,oldCityId,newCityId,null,null);
                    }
                    for(Method doMethod : updateCountyMethods) {
                        // 调用方法执行
                        doMethod.invoke(zoningMapper,nativeSevenName,newCountyId,oldCountyId,null);
                    }
                    for(Method updateVillMethod : updateVillMethods) {
                        // 调用方法执行
                        updateVillMethod.invoke(zoningMapper,nativeNineName,codeId,codeId2,nativeSevenName,newCountyId,oldCountyId);
                    }
                    // 删除行政区划
                    List<Map<String,Object>> systemCodeMap = systemCodeMapper.selectSystemCodeByCodeId(codeId,null);
                    if(!CollectionUtils.isEmpty(systemCodeMap)) {
                        for(Map<String,Object> stringObjectMap : systemCodeMap) {
                            zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId2,Constants.NumberStr.NUM_STR_FIVE,mergeUuid);
                        }
                    }
                    systemCodeMapper.deleteTbsysCodeById(codeId);
                    // 删除部门
                    List<Map<String,Object>> departmentMap = departmentMapper.selectDepartmentById(codeId,null);
                    if(!CollectionUtils.isEmpty(departmentMap)) {
                        for(Map<String,Object> stringObjectMap : departmentMap) {
                            zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId2,Constants.NumberStr.NUM_STR_FIVE,mergeUuid);
                        }
                    }
                    departmentMapper.deleteById(codeId);
                    // 删除auth_user
                    List<Map<String,Object>> authUserMap = authUserMapper.selectAuthUserByCodeId(codeId);
                    if(!CollectionUtils.isEmpty(authUserMap)) {
                        for(Map<String,Object> stringObjectMap : authUserMap) {
                            zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId2,Constants.NumberStr.NUM_STR_FIVE,mergeUuid);
                        }
                    }
                    authUserMapper.deleteAccountByDepartId(codeId);
                    // 删除用户
                    List<Map<String,Object>> systemUserMap = systemUserMapper.selectSystemUserByDepartmentId(codeId,null);
                    if(!CollectionUtils.isEmpty(systemUserMap)) {
                        for(Map<String,Object> stringObjectMap : systemUserMap) {
                            zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId2,Constants.NumberStr.NUM_STR_FIVE,mergeUuid);
                        }
                    }
                    systemUserMapper.deleteAccountByDepartId(codeId);
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CLCG);
                } else {
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CODEERROR);
                }
            } else {
                zoneInfo.setResultOneMessage(Constants.ZHCN.CXSB);
            }
        } catch(Exception e) {
            zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
            log.debug("任务:乡镇/街道行政区划合并,发生异常:{},事务回滚",e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 行政区划:行政区划删除
     * @param zoneInfo zoneInfo
     * @param mergeUuid mergeUuid
     */
    @DS("db2")
    @Transactional(transactionManager = "db2TransactionManager", rollbackFor = Exception.class)
    public void toDeleteXzqh(ZoneInfo zoneInfo,String mergeUuid) {
        Map<String,Object> codeMap = null;
        String optionName = zoneInfo.getOptionName();
        String nativeOneName = zoneInfo.getNativeOneName();
        String nativeTwoName = zoneInfo.getNativeTwoName();
        String nativeThreeName = zoneInfo.getNativeThreeName();
        String nativeFourName = zoneInfo.getNativeFourName();
        try {
            if(Constants.ZHCN.QXXZQHSC.equals(optionName)) {
                try {
                    /**
                     * 描述: nativeOneName --> nativeProName
                     *      nativeTwoName --> nativeCityName
                     *      nativeThreeName --> nativeCouName
                     *      nativeFourName --> nativeVillName
                     */
                    codeMap = jdbcTemplate.queryForMap("  SELECT c.code_id as codeId,a.code_id as proId FROM tbsys_code a LEFT JOIN tbsys_code b ON a.code_id = b.pcode_id LEFT JOIN tbsys_code c ON b.code_id = c.pcode_id  WHERE a.code_type = '2_1' AND a.code_name = \'" + nativeOneName + "\' AND b.code_name = \'" + nativeTwoName + "\' AND c.code_name = \'" + nativeThreeName + "\';");
                } catch(DataAccessException e) {
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                    log.info("任务:删除区/县行政区划,数据库中未查询到信息:{},省:{},市:{},区:{}",e,nativeOneName,nativeTwoName,nativeThreeName);
                }
            } else if(Constants.ZHCN.JDXZQHSC.equals(optionName)) {
                try {
                    /**
                     * 描述: nativeOneName --> nativeProName
                     *      nativeTwoName --> nativeCityName
                     *      nativeThreeName --> nativeCouName
                     */
                    codeMap = jdbcTemplate.queryForMap("SELECT d.code_id AS codeId,a.code_id as proId FROM tbsys_code a LEFT JOIN tbsys_code b ON a.code_id = b.pcode_id LEFT JOIN tbsys_code c ON b.code_id = c.pcode_id LEFT JOIN tbsys_code d ON c.code_id = d.pcode_id WHERE a.code_type = '2_1' AND a.code_name = \'" + nativeOneName + "\' AND b.code_name = \'" + nativeTwoName + "\' AND c.code_name = \'" + nativeThreeName + "\' AND d.code_name = \'" + nativeFourName + "\'");
                } catch(DataAccessException e) {
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                    log.info("任务:删除乡镇/街道行政区划,数据库中未查询到信息:{},省:{},市:{},区:{},街道:{}",e,nativeOneName,nativeTwoName,nativeThreeName,nativeFourName);
                }
            }
            if(null != codeMap && !codeMap.isEmpty()) {
                String proId = (String)codeMap.get("proId");
                String proId1 = (String)codeMap.get("proId");
                if(proId.equals(proId1) && localCode.equals(proId1)) {
                    String codeId = (String)codeMap.get("codeId");
                    // 查找当前账号下是否存在数据
                    Integer mainCount = null;
                    Integer abpCount = null;
                    Map<String,String> paramMap = new HashMap<>(16);
                    paramMap.put("table_name","prisoner_main");
                    paramMap.put("value1",codeId);
                    if(Constants.ZHCN.QXXZQHSC.equals(optionName)) {
                        paramMap.put("field1","nativecou");
                        commonMapper.existeNum(paramMap);
                        paramMap.put("table_name","ab_prisoners");
                        abpCount = commonMapper.existeNum(paramMap);
                    } else if(Constants.ZHCN.JDXZQHSC.equals(optionName)) {
                        paramMap.put("field1","nativevill");
                        mainCount = commonMapper.existeNum(paramMap);
                        paramMap.put("table_name","ab_prisoners");
                        abpCount = commonMapper.existeNum(paramMap);
                    }
                    String mergeJson = JsonUtil.toText(zoneInfo);
                    // 如果main表与ab表的数据都不存在才删除
                    if(Constants.Number.NUM_ZERO.equals(mainCount) && Constants.Number.NUM_ZERO.equals(abpCount)) {
                        // 删除行政区划
                        List<Map<String,Object>> systemCodeMap = systemCodeMapper.selectSystemCodeByCodeId(codeId,null);
                        if(!CollectionUtils.isEmpty(systemCodeMap)) {
                            for(Map<String,Object> stringObjectMap : systemCodeMap) {
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId,Constants.NumberStr.NUM_STR_FIVE,mergeUuid);
                            }
                        }
                        systemCodeMapper.deleteTbsysCodeById(codeId);
                        // 删除部门
                        List<Map<String,Object>> departmentMap = departmentMapper.selectDepartmentById(codeId,null);
                        if(!CollectionUtils.isEmpty(departmentMap)) {
                            for(Map<String,Object> stringObjectMap : departmentMap) {
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId,Constants.NumberStr.NUM_STR_FIVE,mergeUuid);
                            }
                        }
                        departmentMapper.deleteById(codeId);
                        // 删除auth_user
                        List<Map<String,Object>> authUserMap = authUserMapper.selectAuthUserByCodeId(codeId);
                        if(!CollectionUtils.isEmpty(authUserMap)) {
                            for(Map<String,Object> stringObjectMap : authUserMap) {
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId,Constants.NumberStr.NUM_STR_FIVE,mergeUuid);
                            }
                        }
                        authUserMapper.deleteAccountByDepartId(codeId);
                        // 删除用户
                        List<Map<String,Object>> systemUserMap = systemUserMapper.selectSystemUserByDepartmentId(codeId,null);
                        if(!CollectionUtils.isEmpty(systemUserMap)) {
                            for(Map<String,Object> stringObjectMap : systemUserMap) {
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,codeId,codeId,Constants.NumberStr.NUM_STR_FIVE,mergeUuid);
                            }
                        }
                        systemUserMapper.deleteAccountByDepartId(codeId);
                        zoneInfo.setResultOneMessage(Constants.ZHCN.CLCG);
                    }
                } else {
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CODEERROR);
                }
            } else {
                zoneInfo.setResultOneMessage(Constants.ZHCN.CXSB);
            }
        } catch(Exception e) {
            zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
            if(Constants.ZHCN.QXXZQHSC.equals(optionName)) {
                log.debug("任务:删除区/县行政区划,发生异常:{},事务回滚",e);
            } else if(Constants.ZHCN.JDXZQHSC.equals(optionName)) {
                log.debug("任务:删除乡镇/街道行政区划,发生异常:{},事务回滚",e);
            }
            throw new RuntimeException(e);
        }
    }

    /**
     * 区县行政区划迁移
     * @param zoneInfo zoneInfo
     * @param mergeUuid uuid
     */
    @DS("db2")
    @Transactional(transactionManager = "db2TransactionManager", rollbackFor = Exception.class)
    public void toTransferQx(ZoneInfo zoneInfo,String mergeUuid) {
        // 将区县合并至其他地市
        // 1.获取区县信息是否存在
        // 2.获取迁移至地市是否存在
        // 3.更改区县tbsys_code,tbsys_user,tbsys_department的上级
        // 4.将迁移前地市信息查询出来
        // 5.更改地市信息
        Map<String,Object> codeMap = null;
        Map<String,Object> codeMap2 = null;
        String nativeOneName = zoneInfo.getNativeOneName();
        String nativeTwoName = zoneInfo.getNativeTwoName();
        String nativeThreeName = zoneInfo.getNativeThreeName();
        String nativeFourName = zoneInfo.getNativeFourName();
        String nativeFiveName = zoneInfo.getNativeFiveName();
        try {
            /**
             * 描述: nativeOneName --> nativeProName
             *      nativeTwoName --> nativeCityName
             *      nativeThreeName --> nativeCouName
             *      nativeFourName --> transferProName
             *      nativeFiveName --> transferCityName
             */
            try {
                codeMap = jdbcTemplate.queryForMap("SELECT c.code_id as countyId,b.code_id AS cityId,a.code_id as proId FROM tbsys_code a LEFT JOIN tbsys_code b ON a.code_id = b.pcode_id LEFT JOIN tbsys_code c ON b.code_id = c.pcode_id  WHERE a.code_type = '2_1' AND a.code_name = \'" + nativeOneName + "\' AND b.code_name = \'" + nativeTwoName + "\' AND c.code_name = \'" + nativeThreeName + "\'");
            } catch(DataAccessException e) {
                zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                log.info("任务:区县行政区划迁移:发起方,数据库中未查询到信息:{},省:{},市:{},区:{}",e,nativeOneName,nativeTwoName,nativeThreeName);
            }
            try {
                codeMap2 = jdbcTemplate.queryForMap("SELECT b.code_id AS cityId, a.code_id AS proId FROM tbsys_code a LEFT JOIN tbsys_code b ON a.code_id = b.pcode_id  WHERE a.code_type = '2_1' AND a.code_name = \'" + nativeFourName + "\' AND b.code_name = \'" + nativeFiveName + "\'");
            } catch(DataAccessException e) {
                zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                log.info("任务:区县行政区划迁移:迁移到,数据库中未查询到信息:{},省:{},市:{}",e,nativeFourName,nativeFiveName);
            }
            // 判断迁移前区划与即将迁移区划是否存在
            if(null != codeMap && null != codeMap2 && !codeMap.isEmpty() && !codeMap2.isEmpty()) {
                String proId = (String)codeMap.get("proId");
                String proId1 = (String)codeMap2.get("proId");
                if(proId.equals(proId1) && localCode.equals(proId1)) {
                    String countyId = (String)codeMap.get("countyId");
                    String oldCityId = (String)codeMap.get("cityId");
                    String newCityId = (String)codeMap2.get("cityId");
                    String mergeJson = JsonUtil.toText(zoneInfo);
                    // 反射获取执行方法
                    Class<ZoningMapper> zoningMapperClass = ZoningMapper.class;
                    Method[] methods = zoningMapperClass.getMethods();
                    List<Method> updateCityMethods = new ArrayList<>();
                    List<Method> selectCityMethods = new ArrayList<>();
                    for(Method method : methods) {
                        String name = method.getName();
                        if(name.contains("updateCityName")) {
                            updateCityMethods.add(method);
                        } else if(name.contains("selectCity")) {
                            selectCityMethods.add(method);
                        }
                    }
                    for(Method selectMethod : selectCityMethods) {
                        // 获取方法的返回值
                        List<Map<String,Object>> invoke = (List<Map<String,Object>>)selectMethod.invoke(zoningMapper,oldCityId,null,countyId);
                        if(!CollectionUtils.isEmpty(invoke)) {
                            for(Map<String,Object> stringObjectMap : invoke) {
                                // 向area_merge_log表中插入日志信息
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,oldCityId,newCityId,Constants.NumberStr.NUM_STR_SIX,mergeUuid);
                            }
                        }
                    }
                    for(Method updateCityMethod : updateCityMethods) {
                        // 调用方法执行
                        updateCityMethod.invoke(zoningMapper,nativeFiveName,oldCityId,newCityId,null,countyId);
                    }
                    // 执行成功后更改tbsys_code,tbsys_user,tbsys_department上级单位
                    List<Map<String,Object>> systemCodeMap = systemCodeMapper.selectSystemCodeByCodeId(countyId,null);
                    if(!CollectionUtils.isEmpty(systemCodeMap)) {
                        for(Map<String,Object> stringObjectMap : systemCodeMap) {
                            // 向area_merge_log表中插入日志信息
                            zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,oldCityId,newCityId,Constants.NumberStr.NUM_STR_SIX,mergeUuid);
                        }
                    }
                    SystemCode systemCode = new SystemCode();
                    systemCode.setPcodeId(newCityId);
                    Map<String,String> codeHashMap = new HashMap<>(16);
                    codeHashMap.put("codeId",countyId);
                    systemCodeMapper.updateSystemCode(systemCode,codeHashMap);
                    List<Map<String,Object>> systemUserMap = systemUserMapper.selectSystemUserByDepartmentId(countyId,null);
                    if(!CollectionUtils.isEmpty(systemUserMap)) {
                        for(Map<String,Object> stringObjectMap : systemUserMap) {
                            // 向area_merge_log表中插入日志信息
                            zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,oldCityId,newCityId,Constants.NumberStr.NUM_STR_SIX,mergeUuid);
                        }
                    }
                    SystemUser systemUser = new SystemUser();
                    systemUser.setOrgid(newCityId);
                    Map<String,String> userMap = new HashMap<>(16);
                    userMap.put("departid",countyId);
                    systemUserMapper.updateSystemUser(systemUser,userMap);
                    List<Map<String,Object>> departmentMap = departmentMapper.selectDepartmentById(countyId,null);
                    if(!CollectionUtils.isEmpty(departmentMap)) {
                        for(Map<String,Object> stringObjectMap : departmentMap) {
                            // 向area_merge_log表中插入日志信息
                            zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,oldCityId,newCityId,Constants.NumberStr.NUM_STR_SIX,mergeUuid);
                        }
                    }
                    Department department = new Department();
                    department.setPid(newCityId);
                    Map<String,String> dmMap = new HashMap<>(16);
                    dmMap.put("id",countyId);
                    departmentMapper.updateDepartmentById(department,dmMap);
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CLCG);
                } else {
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CODEERROR);
                }
            } else {
                zoneInfo.setResultOneMessage(Constants.ZHCN.CXSB);
            }
        } catch(Exception e) {
            zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
            log.debug("任务:区县行政区划迁移,发生异常:{},事务回滚",e);
            throw new RuntimeException(e);
        }
    }

    /**
     * 街道行政区划迁移
     * @param zoneInfo zoneInfo
     * @param mergeUuid mergeUuid
     */
    @DS("db2")
    @Transactional(transactionManager = "db2TransactionManager", rollbackFor = Exception.class)
    public void toTransferJd(ZoneInfo zoneInfo,String mergeUuid) {
        /**
         * 描述: nativeOneName --> nativeProName
         *      nativeTwoName --> nativeCityName
         *      nativeThreeName --> nativeCouName
         *      nativeFourName --> nativeVillName
         *      nativeFiveName --> transferProName
         *      nativeSixName --> transferCityName
         *      nativeSevenName --> transferCouName
         */
        Map<String,Object> codeMap = null;
        Map<String,Object> codeMap2 = null;
        String nativeOneName = zoneInfo.getNativeOneName();
        String nativeTwoName = zoneInfo.getNativeTwoName();
        String nativeThreeName = zoneInfo.getNativeThreeName();
        String nativeFourName = zoneInfo.getNativeFourName();
        String nativeFiveName = zoneInfo.getNativeFiveName();
        String nativeSixName = zoneInfo.getNativeSixName();
        String nativeSevenName = zoneInfo.getNativeSevenName();
        try {
            try {
                codeMap = jdbcTemplate.queryForMap("SELECT d.code_id as codeId,c.code_id as countyId,b.code_id as cityId,a.code_id as proId FROM tbsys_code a LEFT JOIN tbsys_code b ON a.code_id = b.pcode_id LEFT JOIN tbsys_code c ON b.code_id = c.pcode_id LEFT JOIN tbsys_code d ON c.code_id = d.pcode_id WHERE a.code_type = '2_1' AND a.code_name = \'" + nativeOneName + "\' AND b.code_name = \'" + nativeTwoName + "\' AND c.code_name = \'" + nativeThreeName + "\' AND d.code_name = \'" + nativeFourName + "\'");
            } catch(DataAccessException e) {
                zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                log.info("任务:乡镇/街道行政区划迁移:发起方,数据库中未查询到信息:{},省:{},市:{},区:{},街道:{}",e,nativeOneName,nativeTwoName,nativeThreeName,nativeFourName);
            }
            try {
                codeMap2 = jdbcTemplate.queryForMap("SELECT c.code_id AS countyId, b.code_id AS cityId,a.code_id as proId FROM tbsys_code a LEFT JOIN tbsys_code b ON a.code_id = b.pcode_id LEFT JOIN tbsys_code c ON b.code_id = c.pcode_id  WHERE a.code_type = '2_1' AND a.code_name = \'" + nativeFiveName + "\' AND b.code_name = \'" + nativeSixName + "\' AND c.code_name = \'" + nativeSevenName + "\'");
            } catch(DataAccessException e) {
                zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
                log.info("任务:乡镇/街道行政区划迁移:迁移到,数据库中未查询到信息:{},省:{},市:{},区:{}",e,nativeFiveName,nativeSixName,nativeSevenName);
            }
            if(null != codeMap && null != codeMap2 && !codeMap.isEmpty() && !codeMap2.isEmpty()) {
                String proId = (String)codeMap.get("proId");
                String proId1 = (String)codeMap2.get("proId");
                if(proId.equals(proId1) && localCode.equals(proId1)) {
                    String codeId = (String)codeMap.get("codeId");
                    String oldCityId = (String)codeMap.get("cityId");
                    String newCityId = (String)codeMap2.get("cityId");
                    String oldCountyId = (String)codeMap.get("countyId");
                    String newCountyId = (String)codeMap2.get("countyId");
                    String mergeJson = JsonUtil.toText(zoneInfo);
                    // 反射获取执行方法
                    Class<ZoningMapper> zoningMapperClass = ZoningMapper.class;
                    Method[] methods = zoningMapperClass.getMethods();
                    List<Method> updateCityMethods = new ArrayList<>();
                    List<Method> selectCityMethods = new ArrayList<>();
                    List<Method> updateCountyMethods = new ArrayList<>();
                    List<Method> selectCountyMethods = new ArrayList<>();
                    for(Method method : methods) {
                        String name = method.getName();
                        if(name.contains("updateCityName")) {
                            updateCityMethods.add(method);
                        } else if(name.contains("selectCity")) {
                            selectCityMethods.add(method);
                        } else if(name.contains("mergeCounty")) {
                            updateCountyMethods.add(method);
                        } else if(name.contains("selectCounty")) {
                            selectCountyMethods.add(method);
                        }
                    }
                    for(Method selectMethod : selectCityMethods) {
                        // 获取方法的返回值
                        List<Map<String,Object>> invoke = (List<Map<String,Object>>)selectMethod.invoke(zoningMapper,oldCityId,codeId,null);
                        if(!CollectionUtils.isEmpty(invoke)) {
                            for(Map<String,Object> stringObjectMap : invoke) {
                                // 向area_merge_log表中插入日志信息
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,oldCityId,newCityId,Constants.NumberStr.NUM_STR_SEVEN,mergeUuid);
                            }
                        }
                    }
                    for(Method updateCityMethod : updateCityMethods) {
                        // 调用方法执行
                        updateCityMethod.invoke(zoningMapper,nativeSixName,oldCityId,newCityId,codeId,null);
                    }
                    for(Method selectCountyMethod : selectCountyMethods) {
                        // 获取方法的返回值
                        List<Map<String,Object>> invoke = (List<Map<String,Object>>)selectCountyMethod.invoke(zoningMapper,oldCountyId,codeId);
                        if(!CollectionUtils.isEmpty(invoke)) {
                            for(Map<String,Object> stringObjectMap : invoke) {
                                // 向area_merge_log表中插入日志信息
                                zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,oldCountyId,newCountyId,Constants.NumberStr.NUM_STR_SEVEN,mergeUuid);
                            }
                        }
                    }
                    for(Method doMethod : updateCountyMethods) {
                        // 调用方法执行
                        doMethod.invoke(zoningMapper,nativeSevenName,newCountyId,oldCountyId,codeId);
                    }
                    // 执行成功后更改tbsys_code,tbsys_user,tbsys_department上级单位
                    List<Map<String,Object>> systemCodeMap = systemCodeMapper.selectSystemCodeByCodeId(codeId,null);
                    if(!CollectionUtils.isEmpty(systemCodeMap)) {
                        for(Map<String,Object> stringObjectMap : systemCodeMap) {
                            // 向area_merge_log表中插入日志信息
                            zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,oldCountyId,newCountyId,Constants.NumberStr.NUM_STR_SEVEN,mergeUuid);
                        }
                    }
                    SystemCode systemCode = new SystemCode();
                    systemCode.setPcodeId(newCountyId);
                    Map<String,String> codeHashMap = new HashMap<>(16);
                    codeHashMap.put("codeId",codeId);
                    systemCodeMapper.updateSystemCode(systemCode,codeHashMap);
                    List<Map<String,Object>> systemUserMap = systemUserMapper.selectSystemUserByDepartmentId(codeId,null);
                    if(!CollectionUtils.isEmpty(systemUserMap)) {
                        for(Map<String,Object> stringObjectMap : systemUserMap) {
                            // 向area_merge_log表中插入日志信息
                            zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,oldCountyId,newCountyId,Constants.NumberStr.NUM_STR_SEVEN,mergeUuid);
                        }
                    }
                    SystemUser systemUser = new SystemUser();
                    systemUser.setOrgid(newCountyId);
                    Map<String,String> userMap = new HashMap<>(16);
                    userMap.put("departid",codeId);
                    systemUserMapper.updateSystemUser(systemUser,userMap);
                    List<Map<String,Object>> departmentMap = departmentMapper.selectDepartmentById(codeId,null);
                    if(!CollectionUtils.isEmpty(departmentMap)) {
                        for(Map<String,Object> stringObjectMap : departmentMap) {
                            // 向area_merge_log表中插入日志信息
                            zoningMapper.saveDataInfo(String.valueOf(stringObjectMap.get("priNum")),String.valueOf(stringObjectMap.get("tableName")),String.valueOf(stringObjectMap.get("field")),JsonUtil.toText(stringObjectMap),mergeJson,oldCountyId,newCountyId,Constants.NumberStr.NUM_STR_SEVEN,mergeUuid);
                        }
                    }
                    Department department = new Department();
                    department.setPid(newCountyId);
                    Map<String,String> dmMap = new HashMap<>(16);
                    dmMap.put("id",codeId);
                    departmentMapper.updateDepartmentById(department,dmMap);
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CLCG);
                } else {
                    zoneInfo.setResultOneMessage(Constants.ZHCN.CODEERROR);
                }
            } else {
                zoneInfo.setResultOneMessage(Constants.ZHCN.CXSB);
            }
            zoneInfo.setResultOneMessage(Constants.ZHCN.CLCG);
        } catch(Exception e) {
            zoneInfo.setResultOneMessage(Constants.ZHCN.CLSB);
            log.debug("任务:街道/乡镇行政区划迁移,发生异常:{},事务回滚",e);
            throw new RuntimeException(e);
        }
    }
}